gpt4 book ai didi

programming-languages - vftable - 这是什么?

转载 作者:行者123 更新时间:2023-12-04 17:39:51 42 4
gpt4 key购买 nike

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

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

有人可以解释一下吗?

最佳答案

它很可能代表“Virtual Function Table ”,并且是某些运行时实现使用的一种机制,以允许 virtual function dispatch .

主流 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]

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]


  • Ellis & Stroustrup 1990,第 227–232 页
  • Heading "Multiple Inheritance"
  • CodeSourcery C++ ABI
  • 关于programming-languages - vftable - 这是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664313/

    42 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com