强制虚拟方法表中的功能顺序? [英] Force the order of functions in the virtual method table?

查看:54
本文介绍了强制虚拟方法表中的功能顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如何控制虚拟表中虚拟函数的顺序?它们的排列顺序是否与声明时相同?

  1. How can I control the order of virtual functions in the virtual table? Are they laid out in the same order that they are declared in?

当使用虚拟表继承类时,继承的类的虚拟表是基类的扩展,还是仅使用继承的类虚拟函数创建的全新虚拟表. (即,虚拟表是否仍在该类的索引+ 0x0上?)

When inheriting a class with a virtual table, is the virtual table of the inherited class an extension of the base class, or is an entirely new virtual table created with only the inherited classes virtual functions. (i.e. is the virtual table still at index +0x0 of the class?)

推荐答案

  1. (a)就标准而言,您不能这样做(实际上甚至不能假设存在vtables). (b)可能,但是您需要在什么情况下控制订单,但无法检查自己?检查的方法是查看虚拟调用的反汇编(并查找添加到vtable指针的偏移量以获取调用地址)或查看vtable本身的反汇编.

  1. (a) As far as the standard is concerned, you can't, (in fact you can't even assume that vtables exist). (b) Probably, but what are the circumstances in which you need to control the order, but you can't check for yourself? The way to check is to look at the disassembly of a virtual call (and find the offset(s) added to the vtable pointer to get the call address) or to look at the disassembly of the vtable itself.

取决于.对于单继承,可能是基类的扩展,每个对象的索引0指向该类的虚拟表,并指向在基类中声明的每个虚拟函数的正确实现(可能是覆盖)的指针.指向派生类中声明的每个虚函数的指针.对于多重继承和虚拟继承来说,并不是那么简单.每个对象将包含多个指针,指向vtables或包含vtables的结构以及其他类信息,并且在围绕类层次结构进行转换时,对象的指针值会更改.试试看.

Depends. For single inheritance, probably it's an extension of the base class, and index 0 of each object points to a virtual table for the class, with pointer to the correct implementation (perhaps an override) for each virtual function declared in base classes, followed by pointers to each virtual function declared in the derived class. For multiple and virtual inheritance it isn't (can't be) that simple. Each object will contain several pointers, either to vtables or to structures which contain vtables plus other class information, and when you cast around the class hierarchy, the pointer value of the object changes. Try it and see.

所有这些都是非常假设的典型实现".编译器编写者有其窍门.

All of this for a very hypothetical, "typical implementation". Compiler-writers have their tricks.

这篇关于强制虚拟方法表中的功能顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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