vftable-这是什么?

发布于 2024-11-19 18:25:40 字数 88 浏览 1 评论 0原文

高级编程语言中的 vftable 是什么?

我读到类似虚拟对象结构的地址之类的内容,但这是一个非常混乱的信息

有人可以解释一下吗?

What is vftable in high programming languages?

I read something like it's the address of a virtual object structure, but this is a pretty messy information

Can someone please explain it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-11-26 18:25:40

它很可能代表“虚拟函数表”,并且是某些运行时实现使用的机制为了允许虚拟函数调度

主流 C++ 实现(GCC、Clang、MSVS)将其称为vtable。 C没有多态性。我只能推测其他语言。


以下是维基百科关于该主题的说法:

对象的调度表将包含对象的地址
动态绑定方法。方法调用是通过获取
对象调度表中方法的地址。调度表
对于属于同一类的所有对象都是相同的,并且
因此通常在它们之间共享。对象属于
类型兼容的类(例如继承中的兄弟姐妹
层次结构)将具有具有相同布局的调度表:地址
给定方法的所有方法将出现在相同的偏移处
类型兼容的类。因此,从a中获取方法的地址
给定调度表偏移量将获取对应的方法
对象的实际类。[1]

C++ 标准并没有明确规定动态分派必须如何进行
已实现,但编译器通常使用相同的微小变化
基本模型。

通常,编译器为每个类创建一个单独的 vtable。什么时候
创建一个对象,指向这个vtable的指针,称为虚拟
表指针、vpointer 或 VPTR,被添加为该表的隐藏成员
对象(成为其第一个成员,除非它成为最后一个[2])。这
编译器还在每个类的构造函数中生成“隐藏”代码
将其对象的 vpointers 初始化为
对应的虚函数表。注意vpointer的位置
对象实例并不是所有编译器的标准,并且依赖于
该位置可能会导致代码不可移植。例如,g++
先前将 vpointer 放置在对象的末尾。[3]

  1. Ellis & Stroustrup 1990,第 227–232 页
  2. 标题“多重继承”< /a>
  3. CodeSourcery C++ ABI

It most likely stands for "Virtual Function Table", and is a mechanism used by some runtime implementations in order to allow virtual function dispatch.

Mainstream C++ implementations (GCC, Clang, MSVS) call it the vtable. C has no polymorphism. I could only speculate about other languages.


Here's what Wikipedia says on the topic:

An object's dispatch table will contain the addresses of the object's
dynamically bound methods. Method calls are performed by fetching the
method's address from the object's dispatch table. The dispatch table
is the same for all objects belonging to the same class, and is
therefore typically shared between them. Objects belonging to
type-compatible classes (for example siblings in an inheritance
hierarchy) will have dispatch tables with the same layout: the address
of a given method will appear at the same offset for all
type-compatible classes. Thus, fetching the method's address from a
given dispatch table offset will get the method corresponding to the
object's actual class.[1]

The C++ standards do not mandate exactly how dynamic dispatch must be
implemented, but compilers generally use minor variations on the same
basic model.

Typically, the compiler creates a separate vtable for each class. When
an object is created, a pointer to this vtable, called the virtual
table pointer, vpointer or VPTR, is added as a hidden member of this
object (becoming its first member unless it's made the last[2]). The
compiler also generates "hidden" code in the constructor of each class
to initialize the vpointers of its objects to the address of the
corresponding vtable. Note that the location of the vpointer in the
object instance is not standard among all compilers, and relying on
the position may result in unportable code. For example, g++
previously placed the vpointer at the end of the object.[3]

  1. Ellis & Stroustrup 1990, pp. 227–232
  2. Heading "Multiple Inheritance"
  3. CodeSourcery C++ ABI
李白 2024-11-26 18:25:40

C++ 标准中没有明确提及 Vftable,但大多数(如果不是全部)实现都使用它来实现虚函数。

对于每个具有虚函数的类,编译器都会创建一个函数指针数组,这些指针指向该类的虚函数的最后一个重写版本。然后每个对象都有一个指向其动态类的vtable的指针。

请参阅此问题及其接受的答案以获取更多说明

虚拟调度实现详细信息

Vftable is not explicitly mentioned in the C++ standard, but most (if not all) implementations use it for virtual function implementation.

For each class with virtual functions the compiler creates an array of function poiners which are the pointers to the last overriden version of the virtual functions of that class. Then each object has a pointer to the vtable of its dynamic class.

See this question and its accepted answer for more illustrations

Virtual dispatch implementation details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文