类(使用单/多继承)的对象有多少个vptr? [英] How many vptr will a object of class(uses single/multiple inheritance) have?

查看:228
本文介绍了类(使用单/多继承)的对象有多少个vptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于clas(child)具有单继承的对象通常需要多少个vptr,其中多个继承base1和base2的基类。什么是确定一个对象有多少vptr提供它有几个单继承和多重继承的策略。虽然标准没有指定关于vptrs,但我只是想知道一个实现如何实现虚函数。

How many vptrs are usually needed for a object whose clas( child ) has single inheritance with a base class which multiple inherits base1 and base2. What is the strategy for identifying how many vptrs a object has provided it has couple of single inheritance and multiple inheritance. Though standard doesn't specify about vptrs but I just want to know how an implementation does virtual function implementation.

推荐答案

您为什么关心?简单的答案是,但我想你想要更完整的东西。

Why do you care? The simple answer is enough, but I guess you want something more complete.

这不是标准的一部分,所以任何实现是免费的但是一般的经验法则是,在使用虚拟表指针作为第零近似的实现中,对于动态分派,您最多需要的虚拟表的指针数量与添加新的虚拟方法到层次结构。 (在某些情况下,虚拟表可以扩展,基础类型和派生类型共享一个 vptr

This is not part of the standard, so any implementation is free to do as they wish, but a general rule of thumb is that in an implementation that uses virtual table pointers, as a zeroth approximation, for the dynamic dispatch you need at most as many pointers to virtual tables as there are classes that add a new virtual method to the hierarchy. (In some cases the virtual table can be extended, and the base and derived types share a single vptr)

// some examples:
struct a { void foo(); };           // no need for virtual table
struct b : a { virtual foo1(); };   // need vtable, and vptr
struct c : b { void bar(); };       // no extra virtual table, 1 vptr (b) suffices
struct d : b { virtual bar(); };    // extra vtable, need b.vptr and d.vptr

struct e : d, b {};                 // 3 vptr, 2 for the d subobject and one for
                                    // the additional b
struct f : virtual b {};
struct g : virtual b {};
struct h : f, g {};                 // single vptr, only b needs vtable and
                                    // there is a single b

基本上,需要自己的动态分派(不能直接重用父类)的每个子对象都需要自己的虚表和vptr。

Basically each subobject of a type that requires its own dynamic dispatch (cannot directly reuse the parents) would need its own virtual table and vptr.

在现实编译器中,将不同的vtables合并到单个vtable。当 d b 中的一组函数中添加一个新的虚函数时,编译器会将潜在的两个表合并因为 d 的vtable将是vtable的扩展版本 b / code>在结尾保留二进制兼容性(即 d vtable可以解释为 b vtable访问 b 中可用的方法, d 对象将有一个 vptr

In reality compilers merge different vtables into a single vtable. When d adds a new virtual function over the set of functions in b, the compiler will merge the potential two tables into a single one by appending the new slots to the end of the vtable, so the vtable for d will be a extended version of the vtable for b with extra elements at the end maintaining binary compatibility (i.e. the d vtable can be interpreted as a b vtable to access the methods available in b), and the d object will have a single vptr.

在多重继承的情况下,事情变得有点复杂,因为每个基地需要有与子对象相同的布局如果它是一个单独的对象,那么将有额外的vptrs指向完整对象的vtable中的不同区域。

In the case of multiple inheritance things become a bit more complicated as each base needs to have the same layout as a subobject of the complete object than if it was a separate object, so there will be extra vptrs pointing to different regions in the complete object's vtable.

最后,在虚拟继承的情况下事情变得更加复杂,并且对于同一个完整的对象可能有多个vtable,vptr在构建/销毁过程中被更新(vptr总是随着构造/销毁进化而更新,但是没有虚拟继承,vptr将指向基础的vtables ,而在虚拟继承的情况下,对于同一类型将有多个vtable)

Finally in the case of virtual inheritance things become even more complicated, and there might be multiple vtables for the same complete object with the vptr's being updated as construction/destruction evolves (vptr's are always updated as construction/destruction evolves, but without virtual inheritance the vptr will point to the base's vtables, while in the case of virtual inheritance there will be multiple vtables for the same type)

这篇关于类(使用单/多继承)的对象有多少个vptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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