c++ - Call extended class function -
so have class called person
class person{ private: public: person(); } and 1 more class called patient
class patient : public person{ private: string text_; public: patient(); void setsomething(string text){ text_ = text; } } now have created array of 5 people like
person *ppl[5]; and added 5 patients in each key of array like
ppl[0] = new patient(); ppl[1] = new patient(); ppl[2] = new patient(); ppl[3] = new patient(); ppl[4] = new patient(); now want call setsomething function from patient class this
ppl[0]->setsomething("test text"); but keep getting following error:
class person has no member named setsomething
you have array of person*. can call public methods of person on elements of array, if point patient objects. able call patient methods, first have cast person* patient*.
person* person = new patient; person->setsomething("foo"); // error! patient* patient = dynamic_cast<patient*>(person); if (patient) { patient->setsomething("foo"); } else { // failed cast. pointee must not patient }
Comments
Post a Comment