Accessing subclass members from a superclass pointer C++

六眼飞鱼酱① 提交于 2019-12-03 16:50:47

You need a dynamic cast:

Student * s = new ...;    // create student of some sort

if ( ResearchStudent * r = dynamic_cast<ReasearchStudent*>( s ) ) {
   r->ResFunc();
}
else if ( CourseStudent * c = dynamic_cast<CourseStudent*>( s ) ) {
   c->CourseFiunc();
}
else {
   throw "unknown student type";
}

Note that this uses type information maintained by the compiler, provided the class has at least one virtual function - if all else fails, make the destructor virtual (as it must be in this case anway). You should always prefer this approach to maintaining your own type information.

The best thing would be to use virtual functions:

class Student
{
   // ...
   virtual void SpecificFunction() = 0; /* = 0 means it's abstract; it must be implemented by a subclass */
   // ...
};

class CourseStudent
{
    void SpecificFunction() { ... }
};

Then you can do:

Student *student;
student->SpecificFunction();

A (worse) alternative can be using dynamic_cast:

Student *student;
CourseStudent *cs = dynamic_cast<CourseStudent *>(student);

if (cs) {
   /* student is a CourseStudent.. */
   cs->SpecificFunction();
}

Virtual functions are inappropriate here because the subclass member functions are specific to those subclasses (for example the CourseStudent has a list of units, whereas a ResearchStudent does not, so a getUnits() function implementation in ResearchStudent would make no sense at all)

I had a bit of a read up on dynamic and static casts ( cplusplus.com typecasting ), and in this instance I think a static cast is more appropriate.

The general disadvantage of a static_cast is that it does not perform any checking at runtime to ensure that the object being cast to a subtype is in fact that subtype and not some other. In this case I am specifically checking the type before I perform the type (using a private data member that is set in the subclass constructor and has no mutator), so as long as my checking is good there should be no problem with a static cast. A static cast is more efficient since a dynamic cast requires more runtime resources to perform the type checking.

Where there is any chance of a member not being the expected type, static casting would not be appropriate so I would go for dynamic casting (this is an assignment so once it has been submitted the code won't need to be maintained, so there's no risk of someone messing it up later).

This is almost certainly a case for using a pure virtual member function in the base class, then overriding in the derived classes where you do the real work.

You need static_cast for that. Since those functions are not virtual members of the base class you can't call them through a pointer-to-base-class pointer. You need to explicitly cast to the actual type of the object.

This problem is usually best solved with virtual functions - you'll not need the object type checking in your code anymore, will have less code and less surface for bugs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!