C++ 多态与虚函数表(三)—— 重复继承

一、前言

个人笔记,不保证严谨和正确☺,如有错误还望指出!

《C++ 多态与虚函数表(一)—— 单继承》

《C++ 多态与虚函数表(二)—— 多继承》

《C++ 多态与虚函数表(三)—— 重复继承》

《C++ 多态与虚函数表(四)—— 虚继承》

《C++ 多态与虚函数表(五)拓展—— 对象大小》

 

二、重复继承

2.1 类定义

class Base
{
public:
    long long ibase;
    Base ():ibase (10) {}
    virtual void f() { cout << "Base::f()" << endl; }
    virtual void g() { cout << "Base::g()" << endl; }
    virtual void h() { cout << "Base::h()" << endl; }
};

class Base1 : public Base
{
public:
    long long ibase1;
    Base1 ():ibase1 (100) {}
    virtual void f() { cout << "Base1::f()" << endl; }
    virtual void g_base1() { cout << "Base1::g_base1()" << endl; }
    virtual void h_base1() { cout << "Base1::h_base1()" << endl; }
};

class Base2 : public Base
{
public:
    long long ibase2;
    Base2 ():ibase2 (200) {}
    virtual void f() { cout << "Base2::f()" << endl; }
    virtual void g() { cout << "Base2::g()" << endl; }
    virtual void h_base2() { cout << "Base2::h_base2()" << endl; }
};

class Derive : public Base1, public Base2
{
public:
    long long iderive;
    Derive():iderive(1000) {}
    virtual void f() { cout << "Derive::f()" << endl; }
    virtual void g_derive() { cout << "Derive::g_derive()" << endl; }
};

2.2 类图

2.3 内存布局

2.4 测试, 64位,G++ 4.6.4版本

// 测试
void test()
{
    typedef void(*ptrType)(void);
    Derive b;
    ptrType pFun = NULL;
    ptrType** pVtab = (ptrType**)&b;
    cout << "sizeof(Derive): " << sizeof(Derive) << endl;

    int index = 0;  // 对象内存访问下标
    auto fun = [&](int basei, int maxj) 
    {
        cout << "[" << index << "] Base" << basei <<"::vptr->" << endl;
        for (int j = 0; j < maxj; ++j)
        {
            pFun = (ptrType)pVtab[index][j];
            cout << "   [" << j <<"] ";
            pFun(); 
        }
        ++index;
        cout << "[" << index << "] Base" << basei <<".ibase = " << (long long)*(&pVtab[index]) << endl; 
        ++index;
        cout << "[" << index << "] Base" << basei <<".ibase = " << (long long)*(&pVtab[index]) << endl; 
        ++index;
    };

    fun(1, 6);
    fun(2, 4);
    cout << "[" << index << "] Parent.iparent = " << (long long)*(&pVtab[index]) << endl;
}

2.5 执行结果

 

三、总结

最顶端的父类Base其成员变量存在于Base1和Base2中,并被Derive给继承下去了。而在Derive中,其有Base1和Base2的实例,于是B的成员在D的实例中存在两份,一份是Base1继承而来的,另一份是B2继承而来的。所以,如果我们使用以下语句,则会产生二义性编译错误:

Derive d;
d.iderive = 0; //二义性错误
d.Base1::ibase = 1; //正确
d.Base2::ibase = 2; //正确

注意,上面例程中的最后两条语句存取的是两个变量。虽然我们消除了二义性的编译错误,但Base类在Derive中还是有两个实例,这种继承造成了数据的重复,我们叫这种继承为重复继承。重复的基类数据成员可能并不是我们想要的。所以,C++引入了虚基类的概念。

 

四、完整代码

#include<iostream>
using namespace std;

class Base
{
public:
    long long ibase;
    Base ():ibase (10) {}
    virtual void f() { cout << "Base::f()" << endl; }
    virtual void g() { cout << "Base::g()" << endl; }
    virtual void h() { cout << "Base::h()" << endl; }
};

class Base1 : public Base
{
public:
    long long ibase1;
    Base1 ():ibase1 (100) {}
    virtual void f() { cout << "Base1::f()" << endl; }
    virtual void g_base1() { cout << "Base1::g_base1()" << endl; }
    virtual void h_base1() { cout << "Base1::h_base1()" << endl; }
};

class Base2 : public Base
{
public:
    long long ibase2;
    Base2 ():ibase2 (200) {}
    virtual void f() { cout << "Base2::f()" << endl; }
    virtual void g() { cout << "Base2::g()" << endl; }
    virtual void h_base2() { cout << "Base2::h_base2()" << endl; }
};

class Derive : public Base1, public Base2
{
public:
    long long iderive;
    Derive():iderive(1000) {}
    virtual void f() { cout << "Derive::f()" << endl; }
    virtual void g_derive() { cout << "Derive::g_derive()" << endl; }
};

// 测试
void test()
{
    typedef void(*ptrType)(void);
    Derive b;
    ptrType pFun = NULL;
    ptrType** pVtab = (ptrType**)&b;
    cout << "sizeof(Derive): " << sizeof(Derive) << endl;

    int index = 0;  // 对象内存访问下标
    auto fun = [&](int basei, int maxj) 
    {
        cout << "[" << index << "] Base" << basei <<"::vptr->" << endl;
        for (int j = 0; j < maxj; ++j)
        {
            pFun = (ptrType)pVtab[index][j];
            cout << "   [" << j <<"] ";
            pFun(); 
        }
        ++index;
        cout << "[" << index << "] Base" << basei <<".ibase = " << (long long)*(&pVtab[index]) << endl; 
        ++index;
        cout << "[" << index << "] Base" << basei <<".ibase = " << (long long)*(&pVtab[index]) << endl; 
        ++index;
    };

    fun(1, 6);
    fun(2, 4);
    cout << "[" << index << "] Parent.iparent = " << (long long)*(&pVtab[index]) << endl;
}

int main()
{
    test();
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值