vftable - 这是什么? [英] vftable - what is this?

查看:76
本文介绍了vftable - 这是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是高级编程语言中的 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

有人可以解释一下吗?

推荐答案

它很可能代表Virtual函数表",是一些运行时实现使用的一种机制,用于允许虚拟函数调度.

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

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

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:

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

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]

C++ 标准并没有明确规定动态调度必须是怎样的已实现,但编译器通常使用相同的微小变化基本模型.

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

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

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. 埃利斯 &Stroustrup 1990,第 227-232 页
  2. 标题为多重继承"立>
  3. CodeSourcery C++ ABI

这篇关于vftable - 这是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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