Possible Duplicate:
How to force child same virtual function call its parent virtual function first
EDIT People are completely missing the point: What I was getting at is if alot of classes inherit Base, I don’t want to have to call Base::myFunction() for every single one!
I’m not really sure how to word this question, but I hope it’s apparent from the code (this may not actually compile, I wrote it quickly):
class Base
{
bool flag = false;
void myFunction ()
{
flag = true;
// here run the inherited class's myFunction()
}
};
class A : public Base
{
void myFunction ()
{
// do some clever stuff without having to set the flags here.
}
};
int main ()
{
A myClass;
myClass.myFunction(); // set the flags and then run the clever stuff
std::cout << myClass.flag << endl; // should print true
return 0;
}
First of all – use virtual functions for the case if you will use pointers.
Then you just need to call base class myFunction in derived class realization.
See example:
If you not likes to call your base class function in all derived classes. You can add special virtual function for “clever” calculations and realize it separately in all derived classes.
Example: