简体   繁体   中英

Accessing subclass members from a superclass pointer C++

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these.

I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them.

The problem is, because these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss.

If I have a pointer to Student, is there a way to get a pointer to the subtype of that Student? Would I need to make some sort of fake cast here to get around the compile-time error?

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();
}

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->CourseFunc();
}
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 anyway). You should always prefer this approach to maintaining your own type information.

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).

这几乎可以肯定是在基类中使用纯虚拟成员函数,然后在您执行实际工作的派生类中覆盖的情况。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM