一、前言
个人笔记,不保证严谨和正确☺,如有错误还望指出!
二、重复继承
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;
}