Given the following snippet,
class Base
{
public:
virtual void eval() const
{
std::cout<<"Base Const Eval\n";
}
};
class Derived:public Base
{
public:
void eval()
{
std::cout<<"Derived Non-Const Eval\n";
}
};
int main()
{
Derived d;
Base* pB=&d;
pB->eval(); //This will call the Base eval()
return 0;
}
Why the pB->eval() will call the Base::eval()?
Thank you
This is because one is declared const and the other isn’t. One function is being hidden by the other. The function in Derived is hiding the one in Base because they have the same name while they aren’t the same function.
My compiler gives a warning here, does yours?