Accessing subclass members from a baseclass pointer C++

17,121

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
17,121
David Mason
Author by

David Mason

I have loved coding from the moment I discovered it! I love to learn, so I try to become familiar with as many technologies as possible. The major benefits I reap from this are that I tend not to be overly constrained when selecting the right tool for the job, I can usually provide some insight to my peers when they need help, and I gain a stronger appreciation for the common principles underlying various technologies. I work professionally in web development using all the usual tools. I work across the full stack, but lean towards the frontend and user experience side. When I can find some spare time, I dabble in game development.

Updated on June 09, 2022

Comments

  • David Mason
    David Mason almost 2 years

    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?