C ++:在虚拟方法表中强制函数的顺序? [英] C++: Force the order of functions in the virtual method table?

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

问题描述


  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.

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

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