简体   繁体   English

没有抽象类的虚函数表?

[英]No Virtual Function Table for abstract class?

I am learning about virtual function tables and their representation by analyzing a binary of a simple program written in Visual C++ (with some optimizations on). 我通过分析用Visual C ++编写的简单程序的二进制文件(具有一些优化功能)来学习虚函数表及其表示。

A few days ago I asked this question while being stuck on virtual method table content with identical COMDAT folding on. 几天前,当我被困在虚拟方法表内容上且COMDAT折叠相同时,我问了这个问题

Now I'm stuck on something else: whenever I analyze a class, I need to find its Virtual Method Table. 现在,我陷入了其他困境:每当我分析一个类时,都需要找到其虚拟方法表。 I can do this by finding either its RTTITypeDescriptor or _s_RTTIClassHierarchyDescriptor , finding a cross reference on it, which should lead me to the _RTTICompleteObjectLocator . 我可以通过找到其RTTITypeDescriptor_s_RTTIClassHierarchyDescriptor并在其上找到一个交叉引用来做到这一点,这应该将我_RTTICompleteObjectLocator When I find a cross reference to the Complete Object Locator, it is written just before the VMT (basically -1st entry of the VMT). 当我找到对“完整对象定位器”的交叉引用时,它是在VMT(基本上是VMT的第-1个条目)之前编写的。

This approach works on some classes (their names start with C in my program). 这种方法适用于某些类(在我的程序中,它们的名称以C开头)。 Then there are classes, that are named with I in the beginning and I am able to pair them with other classes starting with C -- for example there is class CClass and it inherits from IClass . 然后有一些类,它们以I开头命名,并且我可以将它们与以C开头的其他类配对-例如,存在类CClass ,它继承自IClass These I -classes are probably serving as interfaces to the C -classes and thus they probably only contain abstract methods. 这些I类可能充当C类的接口,因此它们可能仅包含抽象方法。

By searching a cross reference to Type Descriptor or Class Hierarchy Descriptor of any of the I -classes I cannot find anything -- there is no Complete Object Locator that would lead me to the VMT of the class (that should be full of references to pure_virtual call if I am correct about the all-abstract methods in the I -classes and if I understand correctly what VMT of abstract class looks like). 通过搜索对任何I类的类型描述符或类层次结构描述符的交叉引用, I什么都找不到—没有完整的对象定位器会导致我进入该类的VMT(应该包含对pure_virtual的引用)打电话假如我在所有的抽象方法正确的I -班,如果我理解正确的是什么抽象类的VMT的样子)。

Why do the I -classes have no VMT? 为什么I类没有VMT? Did the compiler optimize it out because it would just be full of references to pure_virtual call and manages it in a different way? 编译器是否对其进行了优化,因为它仅包含对pure_virtual调用的引用,并以其他方式对其进行管理?

These "interfaces" abstract classes probably have need no user written code in any their constructors and destructors (these either have an empty body and no ctor-init-list, or simply are never user defined); 这些“接口”抽象类可能在其构造函数和析构函数中不需要用户编写任何代码(它们具有空的主体且没有ctor-init-list,或者根本就没有用户定义); let's call these pure interface classes . 让我们称这些纯接口类

[Pure interface class: concept related but not identical to Java interfaces that are (were?) defined as having zero implementation code, in any member function. [纯接口类:与概念相关但与在任何成员函数中定义为零实现代码的Java接口不同(但?)。 But be careful with analogies, as Java interfaces inheritance semantic isn't the same as C++ abstract classes inheritance semantic.] 但是要小心一些类比,因为Java接口继承语义与C ++抽象类继承语义不同。]

It means that in practice no used object ever has pure interface class type : no expression ever refers to an object with pure interface type. 这意味着在实践中, 从来没有使用过的对象具有纯接口类类型 :没有表达式会引用具有纯接口类型的对象。 Hence, no vtable is ever needed, so the vtable, which may have been generated during compilation, isn't included in linked code (the linker can see the symbol of the pure interface class vtable isn't used). 因此,不再需要vtable,因此可能在编译期间生成的vtable不包含在链接代码中(链接器可以看到未使用纯接口类vtable的符号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM