下面我们再来看看,发生重复继承的情况。所谓重复继承,也就是某个基类被间接地重复继承了多次。
下图是一个继承图,我们重载了父类的f()函数。
其类继承的源代码如下所示。其中,每个类都有两个变量,一个是整形(4字节),一个是字符(1字节),而且还有自己的虚函数,自己overwrite父类的虚函数。如子类d中,f()覆盖了超类的函数, f1() 和f2() 覆盖了其父类的虚函数,df()为自己的虚函数。
class b
{
public:
int ib;
char cb;
public:
b():ib(0),cb('b') {}
virtual void f() { cout << "b::f()" << endl;}
virtual void bf() { cout << "b::bf()" << endl;}
};
class b1 : public b
{
public:
int ib1;
char cb1;
public:
b1():ib1(11),cb1('1') {}
virtual void f() { cout << "b1::f()" << endl;}
virtual void f1() { cout << "b1::f1()" << endl;}
virtual void bf1() { cout << "b1::bf1()" << endl;}
};
class b2: public b
{
public:
int ib2;
char cb2;
public:
b2():ib2(12),cb2('2') {}
virtual void f() { cout << "b2::f()" << endl;}
virtual void f2() { cout << "b2::f2()" << endl;}
virtual void bf2() { cout << "b2::bf2()" << endl;}
};
class d : public b1, public b2
{
public:
int id;
char cd;
public:
d():id(100),cd('d') {}
virtual void f() { cout << "d::f()" << endl;}
virtual void f1() { cout << "d::f1()" << endl;}
virtual void f2() { cout << "d::f2()" << endl;}
virtual void df() { cout << "d::df()" << endl;}
};
我们用来存取子类内存布局的代码如下所示:(在vc 2003和g 3.4.4下)
typedef void(*fun)(void);
int** pvtab = null;
fun pfun = null;
d d;
pvtab = (int**)&d;
cout << "[0] d::b1::_vptr->" << endl;
pfun = (fun)pvtab[0][0];
cout << " [0] "; pfun();
pfun = (fun)pvtab[0][1];
cout << " [1] "; pfun();
pfun = (fun)pvtab[0][2];
cout << " [2] "; pfun();
pfun = (fun)pvtab[0][3];
cout << " [3] "; pfun();
pfun = (fun)pvtab[0][4];
cout << " [4] "; pfun();
pfun = (fun)pvtab[0][5];
cout << " [5] 0x" << pfun << endl;
cout << "[1] b::ib = " << (int)pvtab[1] << endl;
cout << "[2] b::cb = " << (char)pvtab[2] << endl;
cout << "[3] b1::ib1 = " << (int)pvtab[3] << endl;
cout << "[4] b1::cb1 = " << (char)pvtab[4] << endl;
cout << "[5] d::b2::_vptr->" << endl;
pfun = (fun)pvtab[5][0];
cout << " [0] "; pfun();
pfun = (fun)pvtab[5][1];
cout << " [1] "; pfun();
pfun = (fun)pvtab[5][2];
cout << " [2] "; pfun();
pfun = (fun)pvtab[5][3];
cout << " [3] "; pfun();
pfun = (fun)pvtab[5][4];
cout << " [4] 0x" << pfun << endl;
cout << "[6] b::ib = " << (int)pvtab[6] << endl;
cout << "[7] b::cb = " << (char)pvtab[7] << endl;
cout << "[8] b2::ib2 = " << (int)pvtab[8] << endl;
cout << "[9] b2::cb2 = " << (char)pvtab[9] << endl;
cout << "[10] d::id = " << (int)pvtab[10] << endl;
cout << "[11] d::cd = " << (char)pvtab[11] << endl;
程序运行结果如下:
gcc 3.4.4 |
vc 2003 |
[0] d::b1::_vptr-> [0] d::f() [1] b::bf() [2] d::f1() [3] b1::bf1() [4] d::f2() [5] 0x1 [1] b::ib = 0 [2] b::cb = b [3] b1::ib1 = 11 [4] b1::cb1 = 1 [5] d::b2::_vptr-> [0] d::f() [1] b::bf() [2] d::f2() [3] b2::bf2() [4] 0x0 [6] b::ib = 0 [7] b::cb = b [8] b2::ib2 = 12 [9] b2::cb2 = 2 [10] d::id = 100 [11] d::cd = d |
[0] d::b1::_vptr-> [0] d::f() [1] b::bf() [2] d::f1() [3] b1::bf1() [4] d::df() [5] 0x00000000 [1] b::ib = 0 [2] b::cb = b [3] b1::ib1 = 11 [4] b1::cb1 = 1 [5] d::b2::_vptr-> [0] d::f() [1] b::bf() [2] d::f2() [3] b2::bf2() [4] 0x00000000 [6] b::ib = 0 [7] b::cb = b [8] b2::ib2 = 12 [9] b2::cb2 = 2 [10] d::id = 100 [11] d::cd = d |
下面是对于子类实例中的虚函数表的图:
我们可以看见,最顶端的父类b其成员变量存在于b1和b2中,并被d给继承下去了。而在d中,其有b1和b2的实例,于是b的成员在d的实例中存在两份,一份是b1继承而来的,另一份是b2继承而来的。所以,如果我们使用以下语句,则会产生二义性编译错误:
d d;
d.ib = 0; //二义性错误
d.b1::ib = 1; //正确
d.b2::ib = 2; //正确
注意,上面例程中的最后两条语句存取的是两个变量。虽然我们消除了二义性的编译错误,但b类在d中还是有两个实例,这种继承造成了数据的重复,我们叫这种继承为重复继承。重复的基类数据成员可能并不是我们想要的。所以,c 引入了虚基类的概念。
虚拟继承的出现就是为了解决重复继承中多个间接父类的问题的。钻石型的结构是其最经典的结构。也是我们在这里要讨论的结构:
上述的“重复继承”只需要把b1和b2继承b的语法中加上virtual 关键,就成了虚拟继承,其继承图如下所示:
上图和前面的“重复继承”中的类的内部数据和接口都是完全一样的,只是我们采用了虚拟继承:其省略后的源码如下所示:
class b { ……};
class b1 : virtual public b{ ……};
class b2: virtual public b{ ……};
class d : public b1, public b2{ …… };
在查看d之前,我们先看一看单一虚拟继承的情况。下面是一段在vc 2003下的测试程序:(因为vc 和gcc的内存而局上有一些细节上的不同,所以这里只给出vc 的程序,gcc下的程序大家可以根据我给出的程序自己仿照着写一个去试一试):
int** pvtab = null;
fun pfun = null;
b1 bb1;
pvtab = (int**)&bb1;
cout << "[0] b1::_vptr->" << endl;
pfun = (fun)pvtab[0][0];
cout << " [0] ";
pfun(); //b1::f1();
cout << " [1] ";
pfun = (fun)pvtab[0][1];
pfun(); //b1::bf1();
cout << " [2] ";
cout << pvtab[0][2] << endl;
cout << "[1] = 0x";
cout << (int*)*((int*)(&bb1) 1) <
cout << "[2] b1::ib1 = ";
cout << (int)*((int*)(&bb1) 2) <
cout << "[3] b1::cb1 = ";
cout << (char)*((int*)(&bb1) 3) << endl; //b1::cb1
cout << "[4] = 0x";
cout << (int*)*((int*)(&bb1) 4) << endl; //null
cout << "[5] b::_vptr->" << endl;
pfun = (fun)pvtab[5][0];
cout << " [0] ";
pfun(); //b1::f();
pfun = (fun)pvtab[5][1];
cout << " [1] ";
pfun(); //b::bf();
cout << " [2] ";
cout << "0x" << (fun)pvtab[5][2] << endl;
cout << "[6] b::ib = ";
cout << (int)*((int*)(&bb1) 6) <
cout << "[7] b::cb = ";
其运行结果如下(我结出了gcc的和vc 2003的对比):
gcc 3.4.4 |
vc 2003 |
[0] b1::_vptr -> [0] : b1::f() [1] : b1::f1() [2] : b1::bf1() [3] : 0 [1] b1::ib1 : 11 [2] b1::cb1 : 1 [3] b::_vptr -> [0] : b1::f() [1] : b::bf() [2] : 0 [4] b::ib : 0 [5] b::cb : b [6] null : 0 |
[0] b1::_vptr-> [0] b1::f1() [1] b1::bf1() [2] 0 [1] = 0x00454310 ç该地址取值后是-4 [2] b1::ib1 = 11 [3] b1::cb1 = 1 [4] = 0x00000000 [5] b::_vptr-> [0] b1::f() [1] b::bf() [2] 0x00000000 [6] b::ib = 0 [7] b::cb = b |
这里,大家可以自己对比一下。关于细节上,我会在后面一并再说。
下面的测试程序是看子类d的内存布局,同样是vc 2003的(因为vc 和gcc的内存布局上有一些细节上的不同,而vc 的相对要清楚很多,所以这里只给出vc 的程序,gcc下的程序大家可以根据我给出的程序自己仿照着写一个去试一试):
d d;
pvtab = (int**)&d;
cout << "[0] d::b1::_vptr->" << endl;
pfun = (fun)pvtab[0][0];
cout << " [0] "; pfun(); //d::f1();
pfun = (fun)pvtab[0][1];
cout << " [1] "; pfun(); //b1::bf1();
pfun = (fun)pvtab[0][2];
cout << " [2] "; pfun(); //d::df();
pfun = (fun)pvtab[0][3];
cout << " [3] ";
cout << pfun << endl;
//cout << pvtab[4][2] << endl;
cout << "[1] = 0x";
cout << (int*)((&dd) 1) <
cout << "[2] b1::ib1 = ";
cout << *((int*)(&dd) 2) <
cout << "[3] b1::cb1 = ";
cout << (char)*((int*)(&dd) 3) << endl; //b1::cb1
//---------------------
cout << "[4] d::b2::_vptr->" << endl;
pfun = (fun)pvtab[4][0];
cout << " [0] "; pfun(); //d::f2();
pfun = (fun)pvtab[4][1];
cout << " [1] "; pfun(); //b2::bf2();
pfun = (fun)pvtab[4][2];
cout << " [2] ";
cout << pfun << endl;
cout << "[5] = 0x";
cout << *((int*)(&dd) 5) << endl; // ???
cout << "[6] b2::ib2 = ";
cout << (int)*((int*)(&dd) 6) <
cout << "[7] b2::cb2 = ";
cout << (char)*((int*)(&dd) 7) << endl; //b2::cb2
cout << "[8] d::id = ";
cout << *((int*)(&dd) 8) << endl; //d::id
cout << "[9] d::cd = ";
cout << (char)*((int*)(&dd) 9) << endl;//d::cd
cout << "[10] = 0x";
cout << (int*)*((int*)(&dd) 10) << endl;
//---------------------
cout << "[11] d::b::_vptr->" << endl;
pfun = (fun)pvtab[11][0];
cout << " [0] "; pfun(); //d::f();
pfun = (fun)pvtab[11][1];
cout << " [1] "; pfun(); //b::bf();
pfun = (fun)pvtab[11][2];
cout << " [2] ";
cout << pfun << endl;
cout << "[12] b::ib = ";
cout << *((int*)(&dd) 12) << endl; //b::ib
cout << "[13] b::cb = ";
cout << (char)*((int*)(&dd) 13) <
下面给出运行后的结果(分vc 和gcc两部份)
gcc 3.4.4 |
vc 2003 |
[0] b1::_vptr -> [0] : d::f() [1] : d::f1() [2] : b1::bf1() [3] : d::f2() [4] : d::df() [5] : 1 [1] b1::ib1 : 11 [2] b1::cb1 : 1 [3] b2::_vptr -> [0] : d::f() [1] : d::f2() [2] : b2::bf2() [3] : 0 [4] b2::ib2 : 12 [5] b2::cb2 : 2 [6] d::id : 100 [7] d::cd : d [8] b::_vptr -> [0] : d::f() [1] : b::bf() [2] : 0 [9] b::ib : 0 [10] b::cb : b [11] null : 0 |
[0] d::b1::_vptr-> [0] d::f1() [1] b1::bf1() [2] d::df() [3] 00000000 [1] = 0x0013fdc4 ç 该地址取值后是-4 [2] b1::ib1 = 11 [3] b1::cb1 = 1 [4] d::b2::_vptr-> [0] d::f2() [1] b2::bf2() [2] 00000000 [5] = 0x4539260 ç 该地址取值后是-4 [6] b2::ib2 = 12 [7] b2::cb2 = 2 [8] d::id = 100 [9] d::cd = d [10] = 0x00000000 [11] d::b::_vptr-> [0] d::f() [1] b::bf() [2] 00000000 [12] b::ib = 0 [13] b::cb = b |
关于虚拟继承的运行结果我就不画图了(前面的作图已经让我产生了很严重的厌倦感,所以就偷个懒了,大家见谅了)
在上面的输出结果中,我用不同的颜色做了一些标明。我们可以看到如下的几点:
1)无论是gcc还是vc ,除了一些细节上的不同,其大体上的对象布局是一样的。也就是说,先是b1(黄色),然后是b2(绿色),接着是d(灰色),而b这个超类(青蓝色)的实例都放在最后的位置。
2)关于虚函数表,尤其是第一个虚表,gcc和vc 有很重大的不一样。但仔细看下来,还是vc 的虚表比较清晰和有逻辑性。
3)vc 和gcc都把b这个超类放到了最后,而vc 有一个null分隔符把b和b1和b2的布局分开。gcc则没有。
4)vc 中的内存布局有两个地址我有些不是很明白,在其中我用红色标出了。取其内容是-4。接道理来说,这个指针应该是指向b类实例的内存地址(这个做法就是为了保证重复的父类只有一个实例的技术)。但取值后却不是。这点我目前还并不太清楚,还向大家请教。
5)gcc的内存布局中在b1和b2中则没有指向b的指针。这点可以理解,编译器可以通过计算b1和b2的size而得出b的偏移量。
c 这门语言是一门比较复杂的语言,对于程序员来说,我们似乎永远摸不清楚这门语言背着我们在干了什么。需要熟悉这门语言,我们就必需要了解c 里面的那些东西,需要我们去了解他后面的内存对象。这样我们才能真正的了解c ,从而能够更好的使用c 这门最难的编程语言。