In the following snippet, foo()
is not called directly via the Base
class pointer (so that dynamic binding kicks in), but is called from a member function. Why is the derived class version still called?
#include <iostream>
struct Base{
void run(){
std::cout << "Base run" << "\n";
foo();
}
virtual void foo(){
std::cout << "Base foo" << "\n";
};
};
struct Derived : Base{
void foo() override{
std::cout << "Derived foo" << "\n";
}
};
int main(){
Derived d;
Base* ptr = &d;
ptr->run();
}
Output:
base run
Derived