C++ - Memory Layout of Virtual Inherit (虚继承的内存布局)

Virtual Table是如何实现的呢?

 

1. single inherit的简单情况

 

A virtual table is a static look up array that the compiler sets up at compile time.

A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

 

Second, the compiler also adds a hidden pointer to the classes that have virtual functions or inherit from a base having virtual functions

 

Ref:

Virtual table (http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ )

 

2. multiple inherit/virtual inherit的复杂情况

In gcc some more vptr are added to implementing upcast/downcast, which point to offset value in Virtual table. Refer to:

Memory Layout for Multiple and Virtual Inheritance (gcc http://www.phpcompiler.org/articles/virtualinheritance.html )

 

In Visual C++, virtual inherit will add a vbptr (virtual base table pointer) to each derived classes. Refer to:

C++: Under the Hood by Jan Gray; it talks about class layout techniques and the virtual function call mechanism.

(http://www.openrce.org/articles/files/jangrayhood.pdf )

 

C++: Under the Hood是英文的;有一个中文的站点,详细讲解了包括virtual inherit在内的各种情况的object的layout,很不错!见http://www.cnblogs.com/Binhua-Liu/archive/2010/06/16/1759019.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值