Mechanism of Vptr and Vtable in C++

前端 未结 3 448
囚心锁ツ
囚心锁ツ 2020-12-03 03:58

In C++, during dynamic binding, consider the following example...

class Base
{
  virtual void fun()
  {
     cout<<\"Base\";
  }      
};

class Derive         


        
3条回答
  •  旧时难觅i
    2020-12-03 04:43

    And bptr->fun() will be getting resolved to bptr->vptr->fun();. This points to the base class function itself.

    Wrong. The Derived instance's vptr (a hidden field in each instance) points to the Derived vtable.

提交回复
热议问题