完全纯虚拟类的Vtable放置

发布于 2024-10-10 09:15:11 字数 111 浏览 4 评论 0原文

根据我对 C++ 规范的(有限)了解,具有虚拟成员的类的 vtable 放置在第一个非纯非内联虚拟方法的定义处。编译器如何处理从具有所有纯虚方法(例如接口)的类继承的类?在这种情况下,vtable 放在哪里?

According to my (limited) knowledge of the C++ spec, the vtable of a class with virtual members is placed at the definition of the first non-pure non-inline virtual method. How do compilers handle classes which inherit from a class with ALL pure virtual methods (interfaces, for example)? Where is the vtable placed in this case?

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

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

发布评论

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

评论(3

可遇━不可求 2024-10-17 09:15:11

vtable 存储已实现的虚拟方法的地址。如果一个类的所有方法都是纯虚拟的并且没有实现,则不需要生成vtable。

如果没有从它派生并实现这些方法的一些类,您就无法充分使用这样的类。每个实现了虚拟方法的类都有自己的单个 vtable,其中包含所有虚拟方法的地址:它不以任何方式引用基类的 vtable;地址重复。因此,如果您有一个从另一个类继承的类,则该类将仅使用其自己的 vtable。它不关心基类的vtable;这个虚拟表甚至不需要存在。

C++ 规范本身没有提及 vtable;它们只是一种已经变得普遍的编译器行为。

2020 年编辑:我大约十年前写的。我怀疑我是根据记忆和个人经历写的。下面的两条注释表明编译器确实为基类创建虚表(以抛出错误),尽管如果是这样,我不知道如何为它们构造一个对象,并且某些编译器确实重用了基类。自 2011 年以来似乎没有人添加任何东西,而且我现在认知能力下降,很难再思考太多,所以如果这里的某些细节很重要,请自己做一些研究。

The vtable stores the addresses of the implemented virtual methods. If all methods of a class are pure-virtual and none are implemented, then no vtable needs to be generated.

You could not use such a class for much without some classes which derive from it and implement the methods. Each class with implemented virtual methods has its own single vtable containing addresses for all virtual methods: it does not in any way reference the vtables of base classes; the addresses are duplicated. So if you have a class which inherits from another class, that class will use just its own vtable. It doesn't care about the vtable of the base class; this vtable doesn't even need to exist.

The C++ specification itself says nothing about vtables; they are simply a compiler behaviour which has become common.

EDIT from 2020: I wrote this almost a decade ago. I suspect I wrote it from memory and personal experience. Two comments below indicate that compilers do make vtables for base classes (to throw errors), although if so I don't know how you would construct an object for them, and that some compilers do reuse vtables of base classes. Nobody seems to have added anything since 2011, and I have cognitive decline now and it's hard to really think much anymore, so do some research of your own if some detail here is crucial.

一杆小烟枪 2024-10-17 09:15:11

C++ 标准没有指定有关 v 表放置的任何内容,甚至没有指定 v 表的存在。它只是指定行为,而 v 表恰好是最直接的实现,因此被广泛使用。

实际上,抽象类存在 v 表的原因之一是在构造和销毁期间使用,此时对象的动态类型是抽象类。

在只有纯虚函数的类中,显然不能有构造函数(因为构造函数不能是虚的)。然而,析构函数当然可以是虚拟的。

你的类仍然可以有一个纯虚拟析构函数 有了一个实现,然后需要 v-table(或等效的实现细节)。

但是纯虚函数的实现很少见,并且在定义接口时不会完成。

The C++ standard doesn't specify anything about vtable placement, or even the existence of a v-table. It just specifies behavior, and a v-table happens to be the most straightforward implementation, hence widely used.

Practically speaking, the one reason for a v-table to exist for an abstract class is for use during construction and destruction, when the dynamic type of the object is the abstract class.

In a class with only pure virtual functions, there clearly can be no constructors (since constructors cannot be virtual). However, destructors certainly CAN be virtual.

Your class could still have a pure virtual destructor with an implementation and then the v-table (or equivalent implementation details) is required.

But implementations of pure virtual functions are rare, and wouldn't be done when defining an interface.

赏烟花じ飞满天 2024-10-17 09:15:11

在拥有实例之前,您不需要 vtable。

You don't need a vtable until you have an instance.

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