Consider this code
class Base
{
public:
virtual void print ()
{
std::cout << "Base::print" << std::endl;
}
};
class BaseA : public Base
{
public:
virtual void print ()
{
std::cout << "BaseA::print" << std::endl;
}
};
class Derived : public Base
{
public:
virtual void print ()
{
Base::print (); // <= this will always call Base::print even if I derive from BaseA
std::cout << "Derived::print" << std::endl;
}
};
int main ()
{
Base* a = new Derived;
a->print ();
delete a;
}
From Derived::print I call Base::print which is fine untill I deside to derive my Derived from BaseA instead, whereupon I want of course to call BaseA::print. Changing Base::print to BaseA::print in this particular example is not a problem, but what if I have 20 such virtual functions?
How to ask compiler to call immediate parent’s version of the print whatever that is?
Use a typedef:
Compile-time introspection of a class’s immediate bases is not currently possible, although there are proposals (e.g. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3326.pdf).