【发布时间】:2018-02-26 21:15:04
【问题描述】:
我读了这篇文章: https://shaharmike.com/cpp/vtable-part2/
我不明白为什么在 vtable 中(在文章末尾)我们有这个指针:
0x400918 0x400820 非虚拟 thunk 到 Child::FatherFoo()
但不是直接指向方法 Child::FatherFoo() 的指针?
我假设 Child 的 vtable 与 Father 的 vtable 完全分开。
【问题讨论】:
-
这篇文章很好地解释了这一点我认为
Here’s the solution: the compiler creates a ‘thunk’ method that corrects this and then calls the ‘real’ method. The address of the thunk method will sit under Child’s Father vtable, while the ‘real’ method will be under Child’s vtable.这也起到了这句话In other words, for a given Child c;: (void*)&c != (void*)static_cast<Father*>(&c)的作用,它告诉你为什么需要调整this指针 -
当通过Father*访问FatherFool()时,“this”指针指向示例中的第二个vtable。 thunk 主要用于将“this”指针调整到对象的最顶部,这是一个关键步骤,因为映像 Child::FatherFoo() 可以访问 Mother 或 Child 中的数据成员。
标签: c++ multiple-inheritance vtable virtual-functions vptr