码迷,mamicode.com
首页 > 其他好文 > 详细

Virtual Table

时间:2018-05-14 17:34:25      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:技术分享   called   详解   stat   fir   target   rtu   for   virt   

C++对象模型——吴泰

C/C++杂记

C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现

C++ 多继承和虚继承的内存布局 【已翻译100%】 (虚继承参考,推荐)

图说C++对象模型:对象内存布局详解

VTable Notes on Multiple Inheritance in GCC C++ Compiler v4.0.1

Itanium C++ ABI

What is the VTT for a class? 

===================================================

Why do we not have a virtual constructor in C++?

1.vptr变量是在构造函数中进行初始化的。又因为要想执行虚函数必须通过vptr变量找到虚函数表。(在构造函数初始化vptr变量之前是不会调用虚函数的)如果可以定义虚构造函数,就陷入了先有鸡还是先有蛋的问题。 

2.似乎没有虚构造函数的需求

更多回答请参考 这个链接

虚析构函数

执行任何正常的析构函数:

    • 执行函数体
    • 销毁函数体内的自动存储期的对象
    • 调用自己类的非静态数据成员的析构函数
    • 调用父类的析构函数

把析构函数写成虚函数的作用,和普通虚函数的作用是一样的。

GCC编译的时候,虚表中会有两个虚析构函数。

技术分享图片
The entries for virtual destructors are actually pairs of entries. The first destructor, called the complete object destructor, performs the destruction without calling delete() on the object. The second destructor, called the deleting destructor, calls delete() after destroying the object. Both destroy any virtual bases; a separate, non-virtual function, called the base object destructor, performs destruction of the object but not its virtual base subobjects, and does not call delete().
View Code

VS环境下的编译似乎只有一个虚析构函数。

虚继承

虚继承的实现与具体编译器相关,不同编译器实现有所不同。

class A {
public:
  int a;
  virtual void v();
};

class B : public virtual A {
public:
  int b;
  virtual void w();
};

class C : public virtual A {
public:
  int c;
  virtual void x();
};

class D : public B, public C {
public:
  int d;
  virtual void y();
};

                                   +-----------------------+
                                   |   20 (vbase_offset)   |
                                   +-----------------------+
                                   |     0 (top_offset)    |
                                   +-----------------------+
                                   | ptr to typeinfo for D |
                      +----------> +-----------------------+
d --> +----------+    |            |         B::w()        |
      |  vtable  |----+            +-----------------------+
      +----------+                 |         D::y()        |
      |     b    |                 +-----------------------+
      +----------+                 |   12 (vbase_offset)   |
      |  vtable  |---------+       +-----------------------+
      +----------+         |       |    -8 (top_offset)    |
      |     c    |         |       +-----------------------+
      +----------+         |       | ptr to typeinfo for D |
      |     d    |         +-----> +-----------------------+
      +----------+                 |         C::x()        |
      |  vtable  |----+            +-----------------------+
      +----------+    |            |    0 (vbase_offset)   |
      |     a    |    |            +-----------------------+
      +----------+    |            |   -20 (top_offset)    |
                      |            +-----------------------+
                      |            | ptr to typeinfo for D |
                      +----------> +-----------------------+
                                   |         A::v()        |
                                   +-----------------------+


class A {
public:
  int a;
};

class B : public virtual A {
public:
  int b;
  virtual void w();
};

class C : public virtual A {
public:
  int c;
};

class D : public B, public C {
public:
  int d;
  virtual void y();
};


                                   +-----------------------+
                                   |   20 (vbase_offset)   |
                                   +-----------------------+
                                   |     0 (top_offset)    |
                                   +-----------------------+
                                   | ptr to typeinfo for D |
                      +----------> +-----------------------+
d --> +----------+    |            |         B::w()        |
      |  vtable  |----+            +-----------------------+
      +----------+                 |         D::y()        |
      |     b    |                 +-----------------------+
      +----------+                 |   12 (vbase_offset)   |
      |  vtable  |---------+       +-----------------------+
      +----------+         |       |    -8 (top_offset)    |
      |     c    |         |       +-----------------------+
      +----------+         |       | ptr to typeinfo for D |
      |     d    |         +-----> +-----------------------+
      +----------+
      |     a    |
      +----------+

 vbase_offset指的是到基类A的偏移。

Virtual Table

标签:技术分享   called   详解   stat   fir   target   rtu   for   virt   

原文地址:https://www.cnblogs.com/dirge/p/9024916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!