This question already has an answer here:
- Calling virtual functions inside constructors 13 answers
class Base
{
public:
Base(){Foo();}
~Base(){Foo();}
virtual void Foo(){std::cout<<"base";}
};
class Derived: public Base
{
public:
Derived(){Foo();}
~Derived(){Foo();}
void Foo(){std::cout<<"derived";}
};
//main
{
Derived d;
}
Any idea why this code prints out "base" and "derived"?
I understand the advice is not to put virtual function calls inside constructor or desctructor, I just want to know why the above code would have the behaviour. Thanks