Maybe this was covered in some other topic. But I did not find any satisfactory answer. Could somebody explain me following. I have following code:
#include <iostream>
class Base {
public:
Base() {
foo();
}
virtual void foo() {
std::cout << "Base::foo()" << std::endl;
}
};
class Derived : public Base {
public:
Derived(): Base() {}
virtual void foo() {
std::cout << "Derived::foo()" << std::endl;
}
};
int main() {
Derived* p = new Derived();
}
Now my question is why does the Base creator calls foo method which is in the Base and not in the Derived class, although it is overridden in the Derived class?