Access to subclass elements from C ++ superclass pointer

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

I have a function to go through an array, define a subtype of each student, and then call subtype member functions on it.

The problem is that these functions are not overloaded, they are not found in Student, so the compiler makes noise.

If I have a pointer to Student, is there a way to get a pointer to a subtype of this Student? Do I have to do some fake cast here to get around a compile-time error?

+4
source share
5 answers

You need a dynamic composition:

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"; } 

Please note that this uses the type information supported by the compiler, provided that the class has at least one virtual function - if all else fails, make the destructor virtual (as it should be in this case anyway). You should always prefer this approach to store information about your type.

+6
source

It would be best to use virtual functions:

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

Then you can do:

 Student *student; student->SpecificFunction(); 

Alternative A (worse) can be used by dynamic_cast :

 Student *student; CourseStudent *cs = dynamic_cast<CourseStudent *>(student); if (cs) { /* student is a CourseStudent.. */ cs->SpecificFunction(); } 
+9
source

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

I had a bit of reading on dynamic and static broadcasts ( cplusplus.com typecasting ), in which case I think static casting is more suitable.

The common drawback of static_cast is that it does not perform any checks at runtime to ensure that the object being passed to the subtype is actually a subtype, and not some other. In this case, I specifically check the type before I execute the type (using a private data element that is set in the constructor of the subclass and does not have a mutator), so as long as my check is good, there should be no problems with static casting, Static cast is more efficient because dynamic migration requires more runtime resources for type checking.

If there is a possibility that the member will not be the expected type, static casting will not be suitable, so I would go for dynamic casting (this is the purpose, therefore, after sending it, the code does not need to be supported, so there is no risk that someone will ruin it later) .

+3
source

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

+2
source

For this you need static_cast . Since these functions are not virtual members of a base class, you cannot call them a pointer to a base class. You need to explicitly specify the actual type of the object.

This problem is usually best solved with virtual functions - you no longer need to check the type of object in your code, there will be less code and less surface for errors.

+1
source

Source: https://habr.com/ru/post/1305751/


All Articles