vtable在内存中的哪里存储? [英] Where in memory is vtable stored?

查看:242
本文介绍了vtable在内存中的哪里存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vtable的内存存储在哪里?

Where in memory is vtable stored?

推荐答案

取决于编译器。

在VC ++中,vtable指针存储在对象分配的开始位置,在任何成员数据之前。 (前提是您的类至少具有一个虚拟成员函数。)

In VC++, the vtable pointer stored at the beginning of the object allocation, before any member data. (Provided your class has at least one virtual member function.)

如果您的类是从具有vtable的其他类中继承而来的,则可能还会有多个vtable指针。

There also may be multiple vtable pointers, if your class multiply-inherits from other classes with vtables.

vtables本身是静态分配在您的地址空间中的。

然后该对象布局看起来像(对于C的实例):

Then the object layout looks like (for an instance of C):

A's VTable ptr
A's member variables.
B's Vtable ptr
B's member variables.
C's member variables.

继承权

class A {
  virtual Ax() {}
  int a, b;
};
class B {
  virtual Bx() {}
  int c, d;
};
class C : public A, public B {
  int foo, bar;
};

这篇关于vtable在内存中的哪里存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆