Maybe I am wrong, but this seems to be a very basic question. Suddenly my inheritance chain stopped working. Writing a small basic test application proved that it was me that was wrong (so I can't blame the compiler).
I have a base class, with the default behavior in a virtual function. A child class derives from that and changes the behavior.
#include <iostream>
class Base
{
public:
Base() { print(); }
~Base() {}
protected:
virtual void print() { std::cout << "base\n"; }
};
class Child : public Base
{
public:
Child() {}
~Child() {}
protected:
virtual void print() { std::cout << "child\n"; }
};
int main()
{
Base b;
Child c;
}
This prints:
base
base
When a Child instance is created, why is Base::print() called? I thought that by using the virtual keyword, the function can be replaced for the derived class.
At what point did I get myself confused?