C ++虚拟表布局MI(多继承) [英] C++ virtual table layout of MI(multiple inheritance)

查看:210
本文介绍了C ++虚拟表布局MI(多继承)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看以下C ++代码

class Base1 {  
public:  
    Base1();  
    virtual ~Base1();  
    virtual void speakClearly();  
    virtual Base1 *clone() const;  
protected:  
    float data_Base1;  
};  

class Base2 {  
public:  
    Base2();  
    virtual ~Base2();  
    virtual void mumble();  
    virtual Base2 *clone() const;  
protected:  
    float data_Base2;  
};  

class Derived : public Base1, public Base2 {  
public:  
    Derived();  
    virtual ~Derived();  
    virtual Derived *clone() const;  
protected:  
    float data_Derived;  
}; 

C ++对象模型内部4.2说,Base1,Base2和Base2的虚拟表布局派生类似如下:

The 《Inside of C++ Object Model 》4.2 says that the virtual table layout of class Base1,Base2 and Derived is like this:

>

我的问题是:

类Derived的Base1子对象的虚拟表包含 Base2 :: mumble 。为什么?知道Derived类与Base1共享这个虚表,所以我认为Base2的功能不应该出现在这里。有人告诉我为什么吗? Thx。

The virtual table of the Base1 subObject of class Derived contains Base2::mumble.Why?I know Derived class shared this virtual table with Base1,so I think the function of Base2 should not appear here.Could someone tell me why? Thx.

推荐答案

首先,我要提醒大家,实现多态的解决方案的设计是一个ABI决定超出标准。例如,MSVC和Itanium ABI(后面是gcc,clang,icc,...)有不同的实现方式。

Well, first of all, I'll remind everyone that the design of the solution to implement polymorphism is an ABI decision outside of the Standard. For example, MSVC and the Itanium ABI (followed by gcc, clang, icc, ...) have different ways to implement this.

我认为这是查找的优化。

With that out of the way, I think that this is an optimization for lookup.

每当你有一个 Derived 对象)和查找 mumble 成员,你不需要实际找到 Base2 子对象,但可以直接从 Base1 子对象(其地址与派生子对象相同,因此不涉及算术)。

Whenever you have a Derived object (or one of its descendant) and lookup the mumble member, you do not need to actually find out the Base2 subobject but can directly act from the Base1 subobject (whose address coincides with Derived subobject, so no arithmetic involved).

这篇关于C ++虚拟表布局MI(多继承)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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