高级编程语言中的vftable是什么?
我读到类似虚拟对象结构的地址的内容,但这是一个非常混乱的信息
有人可以解释一下吗?
它很可能代表“虚函数表”,并且是一些运行时实现使用的一种机制,以便允许虚函数调度.
主流 C++ 实现(GCC、Clang、MSVS)将其称为vtable
。 C没有多态性。我只能推测其他语言。
以下是维基百科关于该主题的说法:
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++ 标准并没有明确规定动态分派必须如何进行
已实现,但编译器通常使用相同的微小变化
基本模型。
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]
- 埃利斯和斯特鲁斯特鲁普 1990 年,第 227–232 页
- 标题“多重继承”
- CodeSourcery C++ ABI
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)