I am positing this because after reading many posts and answers I still didn't get my answer. Please kindly flag this as duplicate if it is so.
I understand that in C++, the virtual function is implemented through virtual pointer and virtual table.
However I am not sure how does C++ compiler decipher which virtual pointer to use in runtime?
In below simple case:
class Base {
public:
    virtual foo() {}
}
class Derived: public Base {
public:
    foo() override {}
}
int main() {
  D* p_derived = new Derived();
  B* p_base = p_derived;
  p_derived->foo();
  p_base->foo();
}
I understand that p_derived->foo() will look for Derived::vptr per se for the Derived::vtable (namingly) then Derived::foo(), and p_base->foo() follows the same path as Derived::vptr -> Derived::vtable -> Derived::foo(). But how does p_base->foo() finds Derived::vptr
 even though it's static type is Base*? What prevents p_base from finding Base::vptr instead?
Many Thanks
 
    