C++中成员函数的内存实现

Bru*_*uce 2 c++ inheritance

我在维基百科上阅读了一篇关于虚拟表的文章.

class B1
{
public:
  void f0() {}
  virtual void f1() {}
  int int_in_b1;
};

class B2
{
public:
  virtual void f2() {}
  int int_in_b2;
};

used to derive the following class:

class D : public B1, public B2
{
public:
  void d() {}
  void f2() {}  // override B2::f2()
  int int_in_d;
};
Run Code Online (Sandbox Code Playgroud)

阅读之后,我不禁想知道如何在C++中实现非虚拟成员函数.是否有一个单独的表,如v-table,其中存储了所有函数地址?如果是,那么这个表被调用了什么以及继承期间会发生什么?

如果没有那么编译器如何理解这些语句?

D * d1 = new D;
d1->f0();    // statement 1
Run Code Online (Sandbox Code Playgroud)

编译器如何解释f0()是B1的函数,并且由于D公开继承了D,它可以访问f0().根据文章,编译器将语句1更改为

(*B1::f0)(d)
Run Code Online (Sandbox Code Playgroud)

Amn*_*non 7

非虚拟成员函数的实现类似于接受隐藏this参数的全局函数.编译器在编译时知道基于继承树调用哪个方法,因此不需要运行时表.