在 C++ 中,在动态绑定期间,请考虑以下示例...
class Base
{
  virtual void fun()
  {
     cout<<"Base";
  }      
};
class Derived : public Base
{
   void fun()
   {
     cout<<"Derived";
   }
};
int main()
{
  Base *bptr;
  Derived d;
  bptr=&d;
  bptr->fun();
}
由于声明了虚拟关键字/动态绑定,上述函数的输出为“Derived”。
根据我的理解,将创建一个包含虚拟函数地址的虚拟表(Vtable)。在这种情况下,为派生类创建的虚拟表指向继承的 virtual fun()。并且bptr->fun()将得到解决bptr->vptr->fun();。这指向继承的基类函数本身。我不完全清楚派生类函数是如何调用的?