在运行时访问v表 [英] Access v-table at run-time

查看:113
本文介绍了在运行时访问v表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时可以访问函数的v表吗?可以确定元信息,如不同功能版本的数量?这可能更多是一个理论问题,但是开发人员可以通过确保v表不超过一定数量的行来限制可以扩展给定基类的类数量。

Is it possible to access a function's v-table at runtime? Can meta-information such as the number of different function versions be determined? This might be more of a theoretical question, but could a developer put a cap on the number of classes that can extend a given base class by making sure the v-table never exceeds a certain number of rows?

推荐答案


是否可以在运行时访问函数的v表?是否可以确定元信息,例如不同功能版本的数量?

Is it possible to access a function's v-table at runtime? Can meta-information such as the number of different function versions be determined?

不是以便携式方式。该标准甚至不具有虚拟表的概念,它是一个实现细节比一个需求,即使所有实现我知道使用vtables。在一般情况下,在运行时甚至不会有足够的信息(即,编译器不需要在vtable中存储条目的数量,因为它看到类型并且可以计数)

Not in a portable way. The standard does not even have the concept of virtual table, it is more of an implementation detail than a requirement, even if all implementations I know use vtables. In the general case there will not even be enough information available at runtime (i.e. the compiler does not need to store the number of entries in the vtable, as it sees the type and can count)


开发人员可以通过确保v表不超过一定数量的类来扩展给定的基类的数量上限行?

Could a developer put a cap on the number of classes that can extend a given base class by making sure the v-table never exceeds a certain number of rows?

同样没有,但由于这显示出一种误解,可能值得一试。当一个基类具有任何虚函数时,编译器(在使用vtables的所有实现中)将创建vtable,并且该表将在基类中具有每个虚函数一个条目(加上一些附加数据 - -typeinfo或指向它的指针,偏移到对象的开始或其他实现细节)。当类扩展该基类时,它不会向该vtable添加新元素,而是创建一个单独的vtable(或更多,取决于类型层次结构)。如果派生函数不添加任何新的虚拟函数,则派生对象的vtable将包含原始vtable所具有的元素的确切数目。也就是说,你可以有一个巨大的继承层次,而不影响vtable布局。将会改变的是存储的 typeinfo 数据和指向每个虚函数的指针,它们将引用最终覆盖

Again no, but since this shows a misconception, it might be worth treating it apart. When a base class has any virtual functions the compiler (in all implementations that use vtables) will create the vtable and that table will have exactly 1 entry per virtual function in the base class (plus some additional data --typeinfo or pointer to it, offset to the beginning of the object or other implementation details). When a class extends that base class, it will not add new elements to that vtable, but rather create a separate vtable (or more, depending on the type hierarchy). If the derived function does not add any new virtual function, the vtable for the derived object will contain the exact number of elements that the original vtable had. That is, you can have a huge hierarchy of inheritances without that affecting the vtable layout at all. What will change are the typeinfo data stored and the pointers to each virtual function, that will refer to the final overrider

这篇关于在运行时访问v表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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