一个类在多重或虚拟继承和vtable下的内存布局? [英] Memory layout of a class under multiple or virtual inheritance and the vtable(s)?

查看:207
本文介绍了一个类在多重或虚拟继承和vtable下的内存布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读C ++对象模型内部,试图了解如何通过vtables实现多重和虚拟继承(我理解单一多态性完美)。

I am reading "Inside the C++ Object Model", trying to understand how multiple and virtual inheritance is achieved via the vtables.(I understand single polymorphism perfectly-well).

我有困难了解当一个方法需要在虚拟继承期间或在投放期间定位时,因为要执行大量的偏移计算,所以要做什么。

I am having difficulties understand what exactly is done when a method needs to be located during virtual inheritance, or during casting, because there is a lot of offset calculation to be performed.

有人能够帮助理解多个vtables在多重或虚拟继承示例中如何使用吗?如果我能理解布局和问题,我可能更好地理解这个问题。

Would somebody be able to help with understanding how the multiple vtables are used in a multiple or virtual inheritance example? If I could understand the layout and the problem, I could probably understand this issue better.

推荐答案

C ++实现通常使用vtables虚拟函数。 vtable是一个函数指针表。具有虚拟函数的类的每个对象都具有指向包含类的所有虚拟函数的地址的vtable的隐藏指针。

C++ implementations generally use vtables to implement virtual functions. A vtable is a table of pointers to functions. Each object of a class with virtual functions has a hidden pointer to the vtable containing the addresses of all the virtual functions of the class.

调用虚函数时,代码计算函数指针在vtable中的偏移量,并调用存储在那里的函数。

When invoking a virtual function, the code calculates the offset of the function pointer in the vtable, and calls the function which address is stored there.

>

当基类的派生类覆盖virtuall函数时,该类的虚表只指向覆盖函数,而不是原始函数。

When a derived class of the base class overides a virtuall function, the virtual table of that class just points to the overidden function instead of the original one.

这篇优秀文章解释详细说明它如何工作,无论是单继承还是多继承。

This excellent article explains in details how it work, both for single and multiple inheritance.

这篇关于一个类在多重或虚拟继承和vtable下的内存布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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