的錯誤是:無法訪問受保護的類成員在派生類中
d_start
是CourseActivity
duration
保護成員是CourseActivity
location
保護元件的CourseActivity
保護成員
class CourseActivity{
protected:
StartTime* d_start;
double duration;
std::string location;
public:
CourseActivity() = default;
CourseActivity(const StartTime* _start, double _duration,
const std::string_location);
void reschedule(StartTime* _newStart);
void print() const;
};
class Lecture: public CourseActivity{
std::string topic;
bool deflt = false; //indicate which constructor was used.
//false = 1st. true = 2nd
public:
Lecture(const StartTime* _start, double _duration,
const std::string location, const std::string& _topic);
Lecture(const CourseActivity& _oActivity, const std::string& topic);
void print();
};
// ERROR
Lecture::Lecture(const CourseActivity& _oActivity, const std::string& _topic)
: CourseActivity(_oActivity.d_start,_oActivity.duration,_oActivity.location){
topic = _topic;
deflt = true;
}
// ERROR
[訪問受保護的繼承成員與指向基類的指針]的可能的重複(http://stackoverflow.com/questions/15969658/access-protected-inherited-member-with-pointer-to-base-class) – dyp
什麼導致錯誤?這裏沒有代碼可以做到這一點。 –
您只能從該父項下降的對象訪問父項的受保護方法。在這裏,oActivity不是新創建的對象的父對象,因此其受保護的成員不能被訪問。 – Ashalynd