c++ - 为什么gcc实现的VTT中会有top_offset?

标签 c++ memory-layout virtual-table

这是一个detailed description of VTT在投票最多的答案中。但答案没有解释为什么 VTT 中有 top-offset

从我的观点来看,当我们down_cast一个base指针到derived指针时,编译器已经知道offset需要在编译时调整(没有虚推导时),所以下面的情况不需要存储top_offset:

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

class C : public A, public B {
public:
  int c;
};

In this case, objects of type C are laid out like this (numbers assuming 32-bit pointers):

                           +-----------------------+
                           |     0 (top_offset)    |//why?
                           +-----------------------+
c --> +----------+         | ptr to typeinfo for C |
      |  vtable  |-------> +-----------------------+
      +----------+         |         A::v()        |
      |     a    |         +-----------------------+
      +----------+         |    -8 (top_offset)    |//why?
      |  vtable  |---+     +-----------------------+
      +----------+   |     | ptr to typeinfo for C |
      |     b    |   +---> +-----------------------+
      +----------+         |         B::w()        |
      |     c    |         +-----------------------+
      +----------+

为什么在这种情况下 VTT 中有 top_offset 我认为 top_offsetvirtual base offset仅在虚拟继承中需要。

最佳答案

void *top(B *b) { return dynamic_cast<void *>(b); }

编译器无法在编译时确定正确的偏移量是多少。可以使用空指针、指向完整 B 对象的指针或指向 B 子对象的指针来调用此函数。这三种情况需要区别对待。 vtable 中的偏移量允许它工作。

关于c++ - 为什么gcc实现的VTT中会有top_offset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317010/

相关文章:

c++ - 将 3 个整数的结构 vector 解释为数组

java - 为什么压缩的 Oops 为 Object Header 提供 12 个字节

c++ - 如何正确地将带有 vtable 的类实例写入和读取到 QSharedMemory 中?

javascript - 按 Asc 和 Desc 日期排序

c++ - 插槽后 Qt 访问冲突 0xC0000005

c++ - 使用 .dll 的方法

c++ - 使用 g++ 编译器打印 C++ 对象的布局

C++ SDL "Native' 已退出,代码为 -1073741701 (0xc000007b)”

c++ - 通过C++将数据库表中的记录复制到其他数据库的同名表中

c++ - 虚拟表和 _vptr 存储方案