多重继承中的c ++ vtable,指向thunk方法的指针 [英] c++ vtable in multiple inheritance, pointer to thunk method

查看:35
本文介绍了多重继承中的c ++ vtable,指向thunk方法的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这篇文章:https://shaharmike.com/cpp/vtable-part2/

我不明白为什么在 vtable 中(在文章末尾)我们有这个指针:

And I can not understand why in the vtable (at the end of article) we have this pointer:

0x400918 0x400820 非虚拟 thunk 到 Child::FatherFoo()

0x400918 0x400820 non-virtual thunk to Child::FatherFoo()

但不是直接指向方法 Child::FatherFoo() 的指针?

but not pointer directly to method Child::FatherFoo()?

我假设孩子的 vtable 与父亲的 vtable 完全分开.

I assume that Child's vtable is completely separate from Father's vtable.

推荐答案

就像 C 结构中除了一个成员之外的所有成员不能与包含对象具有相同的地址一样,除了一个非空基类子对象之外的所有子对象都不能具有相同的地址地址作为完整的对象;根据定义,多态基类(带有虚函数的基类)不为空.

Just like all but one members of a C struct cannot have the same address as the encompassing object, all but one non-empty base class subobjects cannot have the same address as the complete object; a polymorphic base class (one with virtual functions) is not empty by definition.

与派生对象具有相同地址的多态基子对象称为主基.派生对象与主基共享 vtable 布局和 vptr 的基:隐式 this 参数未更改.

The polymorphic base subobject that has the same address as the derived object is called the primary base. The derived object shares the base of the vtable layout and the vptr with the primary base: the implicit this parameter is not changed.

注意:primary base的概念是C++实现领域的概念(比如vtable、vptr...),不是C++语言的概念(比如基类、虚函数...).所以,很明显,C++ 标准中没有描述.

Note: The concept of primary base is a C++ implementation domain concept (like vtable, vptr...), not a C++ language concept (like base class, virtual function...). So, obviously, it is not described in the C++ standard.

当动态调用虚函数时,通过虚调用机制,在未知动态类型的对象上,必须将this隐式参数调整为正确的值,即不同的值非主要碱基.执行此操作的中介称为 thunk.在这种情况下,thunk 可以跳转到正确的函数,而不是函数调用:额外的工作发生在函数入口,而在函数退出时什么都不需要.

When a virtual function is called dynamically, via the virtual call mechanism, on a object of an unknown dynamic type, the this implicit argument must be adjusted to the correct value, which is a different value of the non primary bases. The intermediary that does that is called a thunk. In this case, the thunk can do a jump and not a function call, to the correct function: the extra work occurs on function entry, and nothing is needed on function exit.

当使用协变返回类型并且协变返回的派生到基础关系不是派生到主要基础关系时,会发生另一种类型的调整.显然,这种 thunk 不是跳转,而是函数调用,因为协方差的调整发生在函数退出时.

Another type of adjustment occur when a covariant return type is used and the derived to base relation of the covariant return is not a derived to primary base relation. Obviously this kind of thunk doesn't do a jump, it does a function call, as the adjustment for covariance occurs on function exit.

这篇关于多重继承中的c ++ vtable,指向thunk方法的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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