2008-11-14 作者:陳皓 來(lái)源:csdn
前言
07年12月,我寫(xiě)了一篇《C++虛函數(shù)表解析》的文章,引起了大家的興趣。有很多朋友對(duì)我的文章留了言,有鼓勵(lì)我的,有批評(píng)我的,還有很多問(wèn)問(wèn)題的。我在這里一并對(duì)大家的留言表示感謝。這也是我為什么再寫(xiě)一篇續(xù)言的原因。因?yàn)椋谏弦黄恼轮校矣昧说氖纠际欠浅:?jiǎn)單的,主要是為了說(shuō)明一些機(jī)理上的問(wèn)題,也是為了圖一些表達(dá)上方便和簡(jiǎn)單。不想,這篇文章成為了打開(kāi)C++對(duì)象模型內(nèi)存布局的一個(gè)引子,引發(fā)了大家對(duì)C++對(duì)象的更深層次的討論。當(dāng)然,我之前的文章還有很多方面沒(méi)有涉及,從我個(gè)人感覺(jué)下來(lái),在談?wù)撎摵瘮?shù)表里,至少有以下這些內(nèi)容沒(méi)有涉及:
1)有成員變量的情況。
2)有重復(fù)繼承的情況。
3)有虛擬繼承的情況。
4)有鉆石型虛擬繼承的情況。
這些都是我本篇文章需要向大家說(shuō)明的東西。所以,這篇文章將會(huì)是《C++虛函數(shù)表解析》的一個(gè)續(xù)篇,也是一篇高級(jí)進(jìn)階的文章。我希望大家在讀這篇文章之前對(duì)C++有一定的基礎(chǔ)和了解,并能先讀我的上一篇文章。因?yàn)檫@篇文章的深度可能會(huì)比較深,而且會(huì)比較雜亂,我希望你在讀本篇文章時(shí)不會(huì)有大腦思維紊亂導(dǎo)致大腦死機(jī)的情況。;-)
對(duì)象的影響因素
簡(jiǎn)而言之,我們一個(gè)類可能會(huì)有如下的影響因素:
1)成員變量
2)虛函數(shù)(產(chǎn)生虛函數(shù)表)
3)單一繼承(只繼承于一個(gè)類)
4)多重繼承(繼承多個(gè)類)
5)重復(fù)繼承(繼承的多個(gè)父類中其父類有相同的超類)
6)虛擬繼承(使用virtual方式繼承,為了保證繼承后父類的內(nèi)存布局只會(huì)存在一份)
上述的東西通常是C++這門(mén)語(yǔ)言在語(yǔ)義方面對(duì)對(duì)象內(nèi)部的影響因素,當(dāng)然,還會(huì)有編譯器的影響(比如優(yōu)化),還有字節(jié)對(duì)齊的影響。在這里我們都不討論,我們只討論C++語(yǔ)言上的影響。
本篇文章著重討論下述幾個(gè)情況下的C++對(duì)象的內(nèi)存布局情況。
1)單一的一般繼承(帶成員變量、虛函數(shù)、虛函數(shù)覆蓋)
2)單一的虛擬繼承(帶成員變量、虛函數(shù)、虛函數(shù)覆蓋)
3)多重繼承(帶成員變量、虛函數(shù)、虛函數(shù)覆蓋)
4)重復(fù)多重繼承(帶成員變量、虛函數(shù)、虛函數(shù)覆蓋)
5)鉆石型的虛擬多重繼承(帶成員變量、虛函數(shù)、虛函數(shù)覆蓋)
我們的目標(biāo)就是,讓事情越來(lái)越復(fù)雜。
知識(shí)復(fù)習(xí)
我們簡(jiǎn)單地復(fù)習(xí)一下,我們可以通過(guò)對(duì)象的地址來(lái)取得虛函數(shù)表的地址,如:
typedef void(*Fun)(void);
Base b;
Fun pFun = NULL;
cout << "虛函數(shù)表地址:" << (int*)(&b) << endl;
cout << "虛函數(shù)表 — 第一個(gè)函數(shù)地址:" << (int*)*(int*)(&b) << endl;
// Invoke the first virtual function
pFun = (Fun)*((int*)*(int*)(&b));
pFun();
我們同樣可以用這種方式來(lái)取得整個(gè)對(duì)象實(shí)例的內(nèi)存布局。因?yàn)檫@些東西在內(nèi)存中都是連續(xù)分布的,我們只需要使用適當(dāng)?shù)牡刂菲屏浚覀兙涂梢垣@得整個(gè)內(nèi)存對(duì)象的布局。
本篇文章中的例程或內(nèi)存布局主要使用如下編譯器和系統(tǒng):
1)Windows XP 和 VC++ 2003
2)Cygwin 和 G++ 3.4.4
單一的一般繼承
下面,我們假設(shè)有如下所示的一個(gè)繼承關(guān)系:

請(qǐng)注意,在這個(gè)繼承關(guān)系中,父類,子類,孫子類都有自己的一個(gè)成員變量。而了類覆蓋了父類的f()方法,孫子類覆蓋了子類的g_child()及其超類的f()。
我們的源程序如下所示:
class Parent {
public:
int iparent;
Parent ():iparent (10) {}
virtual void f() { cout << " Parent::f()" << endl; }
virtual void g() { cout << " Parent::g()" << endl; }
virtual void h() { cout << " Parent::h()" << endl; }
};
class Child : public Parent {
public:
int ichild;
Child():ichild(100) {}
virtual void f() { cout << "Child::f()" << endl; }
virtual void g_child() { cout << "Child::g_child()" << endl; }
virtual void h_child() { cout << "Child::h_child()" << endl; }
};
class GrandChild : public Child{
public:
int igrandchild;
GrandChild():igrandchild(1000) {}
virtual void f() { cout << "GrandChild::f()" << endl; }
virtual void g_child() { cout << "GrandChild::g_child()" << endl; }
virtual void h_grandchild() { cout << "GrandChild::h_grandchild()" << endl; }
};
我們使用以下程序作為測(cè)試程序:(下面程序中,我使用了一個(gè)int** pVtab 來(lái)作為遍歷對(duì)象內(nèi)存布局的指針,這樣,我就可以方便地像使用數(shù)組一樣來(lái)遍歷所有的成員包括其虛函數(shù)表了,在后面的程序中,我也是用這樣的方法的,請(qǐng)不必感到奇怪,)
typedef void(*Fun)(void);
GrandChild gc;
int** pVtab = (int**)&gc;
cout << "[0] GrandChild::_vptr->" << endl;
for (int i=0; (Fun)pVtab[0][i]!=NULL; i++){
pFun = (Fun)pVtab[0][i];
cout << " ["<<i<<"] ";
pFun();
}
cout << "[1] Parent.iparent = " << (int)pVtab[1] << endl;
cout << "[2] Child.ichild = " << (int)pVtab[2] << endl;
cout << "[3] GrandChild.igrandchild = " << (int)pVtab[3] << endl;
其運(yùn)行結(jié)果如下所示:(在VC++ 2003和G++ 3.4.4下)
[0] GrandChild::_vptr->
[0] GrandChild::f()
[1] Parent::g()
[2] Parent::h()
[3] GrandChild::g_child()
[4] Child::h1()
[5] GrandChild::h_grandchild()
[1] Parent.iparent = 10
[2] Child.ichild = 100
[3] GrandChild.igrandchild = 1000
|
使用圖片表示如下:

可見(jiàn)以下幾個(gè)方面:
1)虛函數(shù)表在最前面的位置。
2)成員變量根據(jù)其繼承和聲明順序依次放在后面。
3)在單一的繼承中,被overwrite的虛函數(shù)在虛函數(shù)表中得到了更新。
多重繼承
下面,再讓我們來(lái)看看多重繼承中的情況,假設(shè)有下面這樣一個(gè)類的繼承關(guān)系。注意:子類只overwrite了父類的f()函數(shù),而還有一個(gè)是自己的函數(shù)(我們這樣做的目的是為了用g1()作為一個(gè)標(biāo)記來(lái)標(biāo)明子類的虛函數(shù)表)。而且每個(gè)類中都有一個(gè)自己的成員變量:
我們的類繼承的源代碼如下所示:父類的成員初始為10,20,30,子類的為100
class Base1 {
public:
int ibase1;
Base1():ibase1(10) {}
virtual void f() { cout << "Base1::f()" << endl; }
virtual void g() { cout << "Base1::g()" << endl; }
virtual void h() { cout << "Base1::h()" << endl; }
};
class Base2 {
public:
int ibase2;
Base2():ibase2(20) {}
virtual void f() { cout << "Base2::f()" << endl; }
virtual void g() { cout << "Base2::g()" << endl; }
virtual void h() { cout << "Base2::h()" << endl; }
};
class Base3 {
public:
int ibase3;
Base3():ibase3(30) {}
virtual void f() { cout << "Base3::f()" << endl; }
virtual void g() { cout << "Base3::g()" << endl; }
virtual void h() { cout << "Base3::h()" << endl; }
};
class Derive : public Base1, public Base2, public Base3 {
public:
int iderive;
Derive():iderive(100) {}
virtual void f() { cout << "Derive::f()" << endl; }
virtual void g1() { cout << "Derive::g1()" << endl; }
};
我們通過(guò)下面的程序來(lái)查看子類實(shí)例的內(nèi)存布局:下面程序中,注意我使用了一個(gè)s變量,其中用到了sizof(Base)來(lái)找下一個(gè)類的偏移量。(因?yàn)槲衣暶鞯氖莍nt成員,所以是4個(gè)字節(jié),所以沒(méi)有對(duì)齊問(wèn)題。關(guān)于內(nèi)存的對(duì)齊問(wèn)題,大家可以自行試驗(yàn),我在這里就不多說(shuō)了)
typedef void(*Fun)(void);
Derive d;
int** pVtab = (int**)&d;
cout << "[0] Base1::_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] "; cout<<pFun<<endl;
cout << "[1] Base1.ibase1 = " << (int)pVtab[1] << endl;
int s = sizeof(Base1)/4;
cout << "[" << s << "] Base2::_vptr->"<<endl;
pFun = (Fun)pVtab[s][0];
cout << " [0] "; pFun();
Fun = (Fun)pVtab[s][1];
cout << " [1] "; pFun();
pFun = (Fun)pVtab[s][2];
cout << " [2] "; pFun();
pFun = (Fun)pVtab[s][3];
out << " [3] ";
cout<<pFun<<endl;
cout << "["<< s+1 <<"] Base2.ibase2 = " << (int)pVtab[s+1] << endl;
s = s + sizeof(Base2)/4;
cout << "[" << s << "] Base3::_vptr->"<<endl;
pFun = (Fun)pVtab[s][0];
cout << " [0] "; pFun();
pFun = (Fun)pVtab[s][1];
cout << " [1] "; pFun();
pFun = (Fun)pVtab[s][2];
cout << " [2] "; pFun();
pFun = (Fun)pVtab[s][3];
cout << " [3] ";
cout<<pFun<<endl;
s++;
cout << "["<< s <<"] Base3.ibase3 = " << (int)pVtab[s] << endl;
s++;
cout << "["<< s <<"] Derive.iderive = " << (int)pVtab[s] << endl;
其運(yùn)行結(jié)果如下所示:(在VC++ 2003和G++ 3.4.4下)
[0] Base1::_vptr->
[0] Derive::f()
[1] Base1::g()
[2] Base1::h()
[3] Driver::g1()
[4] 00000000 ç 注意:在GCC下,這里是1
[1] Base1.ibase1 = 10
[2] Base2::_vptr->
[0] Derive::f()
[1] Base2::g()
[2] Base2::h()
[3] 00000000 ç 注意:在GCC下,這里是1
[3] Base2.ibase2 = 20
[4] Base3::_vptr->
[0] Derive::f()
[1] Base3::g()
[2] Base3::h()
[3] 00000000
[5] Base3.ibase3 = 30
[6] Derive.iderive = 100
|
使用圖片表示是下面這個(gè)樣子:

我們可以看到:
1) 每個(gè)父類都有自己的虛表。
2) 子類的成員函數(shù)被放到了第一個(gè)父類的表中。
3) 內(nèi)存布局中,其父類布局依次按聲明順序排列。
4) 每個(gè)父類的虛表中的f()函數(shù)都被overwrite成了子類的f()。這樣做就是為了解決不同的父類類型的指針指向同一個(gè)子類實(shí)例,而能夠調(diào)用到實(shí)際的函數(shù)。
重復(fù)繼承
下面我們?cè)賮?lái)看看,發(fā)生重復(fù)繼承的情況。所謂重復(fù)繼承,也就是某個(gè)基類被間接地重復(fù)繼承了多次。
下圖是一個(gè)繼承圖,我們重載了父類的f()函數(shù)。

其類繼承的源代碼如下所示。其中,每個(gè)類都有兩個(gè)變量,一個(gè)是整形(4字節(jié)),一個(gè)是字符(1字節(jié)),而且還有自己的虛函數(shù),自己overwrite父類的虛函數(shù)。如子類D中,f()覆蓋了超類的函數(shù), f1() 和f2() 覆蓋了其父類的虛函數(shù),Df()為自己的虛函數(shù)。
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;}
};
我們用來(lái)存取子類內(nèi)存布局的代碼如下所示:(在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;
程序運(yùn)行結(jié)果如下:
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
|
下面是對(duì)于子類實(shí)例中的虛函數(shù)表的圖:

我們可以看見(jiàn),最頂端的父類B其成員變量存在于B1和B2中,并被D給繼承下去了。而在D中,其有B1和B2的實(shí)例,于是B的成員在D的實(shí)例中存在兩份,一份是B1繼承而來(lái)的,另一份是B2繼承而來(lái)的。所以,如果我們使用以下語(yǔ)句,則會(huì)產(chǎn)生二義性編譯錯(cuò)誤:
D d;
d.ib = 0; //二義性錯(cuò)誤
d.B1::ib = 1; //正確
d.B2::ib = 2; //正確
注意,上面例程中的最后兩條語(yǔ)句存取的是兩個(gè)變量。雖然我們消除了二義性的編譯錯(cuò)誤,但B類在D中還是有兩個(gè)實(shí)例,這種繼承造成了數(shù)據(jù)的重復(fù),我們叫這種繼承為重復(fù)繼承。重復(fù)的基類數(shù)據(jù)成員可能并不是我們想要的。所以,C++引入了虛基類的概念。
鉆石型多重虛擬繼承
虛擬繼承的出現(xiàn)就是為了解決重復(fù)繼承中多個(gè)間接父類的問(wèn)題的。鉆石型的結(jié)構(gòu)是其最經(jīng)典的結(jié)構(gòu)。也是我們?cè)谶@里要討論的結(jié)構(gòu):
上述的“重復(fù)繼承”只需要把B1和B2繼承B的語(yǔ)法中加上virtual 關(guān)鍵,就成了虛擬繼承,其繼承圖如下所示:

上圖和前面的“重復(fù)繼承”中的類的內(nèi)部數(shù)據(jù)和接口都是完全一樣的,只是我們采用了虛擬繼承:其省略后的源碼如下所示:
class B {……};
class B1 : virtual public B{……};
class B2: virtual public B{……};
class D : public B1, public B2{ …… };
在查看D之前,我們先看一看單一虛擬繼承的情況。下面是一段在VC++2003下的測(cè)試程序:(因?yàn)閂C++和GCC的內(nèi)存而局上有一些細(xì)節(jié)上的不同,所以這里只給出VC++的程序,GCC下的程序大家可以根據(jù)我給出的程序自己仿照著寫(xiě)一個(gè)去試一試):
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) <<endl; //B1::ib1
cout << "[2] B1::ib1 = ";
cout << (int)*((int*)(&bb1)+2) <<endl; //B1::ib1
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) <<endl; //B::ib
cout << "[7] B::cb = ";
其運(yùn)行結(jié)果如下(我結(jié)出了GCC的和VC++2003的對(duì)比):
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
|
這里,大家可以自己對(duì)比一下。關(guān)于細(xì)節(jié)上,我會(huì)在后面一并再說(shuō)。
下面的測(cè)試程序是看子類D的內(nèi)存布局,同樣是VC++ 2003的(因?yàn)閂C++和GCC的內(nèi)存布局上有一些細(xì)節(jié)上的不同,而VC++的相對(duì)要清楚很多,所以這里只給出VC++的程序,GCC下的程序大家可以根據(jù)我給出的程序自己仿照著寫(xiě)一個(gè)去試一試):
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) <<endl; //????
cout << "[2] B1::ib1 = ";
cout << *((int*)(&dd)+2) <<endl; //B1::ib1
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) <<endl; //B2::ib2
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) <<endl;//B::cb
下面給出運(yùn)行后的結(jié)果(分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
|
關(guān)于虛擬繼承的運(yùn)行結(jié)果我就不畫(huà)圖了(前面的作圖已經(jīng)讓我產(chǎn)生了很?chē)?yán)重的厭倦感,所以就偷個(gè)懶了,大家見(jiàn)諒了)
在上面的輸出結(jié)果中,我用不同的顏色做了一些標(biāo)明。我們可以看到如下的幾點(diǎn):
1)無(wú)論是GCC還是VC++,除了一些細(xì)節(jié)上的不同,其大體上的對(duì)象布局是一樣的。也就是說(shuō),先是B1(黃色),然后是B2(綠色),接著是D(灰色),而B(niǎo)這個(gè)超類(青藍(lán)色)的實(shí)例都放在最后的位置。
2)關(guān)于虛函數(shù)表,尤其是第一個(gè)虛表,GCC和VC++有很重大的不一樣。但仔細(xì)看下來(lái),還是VC++的虛表比較清晰和有邏輯性。
3)VC++和GCC都把B這個(gè)超類放到了最后,而VC++有一個(gè)NULL分隔符把B和B1和B2的布局分開(kāi)。GCC則沒(méi)有。
4)VC++中的內(nèi)存布局有兩個(gè)地址我有些不是很明白,在其中我用紅色標(biāo)出了。取其內(nèi)容是-4。接道理來(lái)說(shuō),這個(gè)指針應(yīng)該是指向B類實(shí)例的內(nèi)存地址(這個(gè)做法就是為了保證重復(fù)的父類只有一個(gè)實(shí)例的技術(shù))。但取值后卻不是。這點(diǎn)我目前還并不太清楚,還向大家請(qǐng)教。
5)GCC的內(nèi)存布局中在B1和B2中則沒(méi)有指向B的指針。這點(diǎn)可以理解,編譯器可以通過(guò)計(jì)算B1和B2的size而得出B的偏移量。
結(jié)束語(yǔ)
C++這門(mén)語(yǔ)言是一門(mén)比較復(fù)雜的語(yǔ)言,對(duì)于程序員來(lái)說(shuō),我們似乎永遠(yuǎn)摸不清楚這門(mén)語(yǔ)言背著我們?cè)诟闪耸裁础P枰煜み@門(mén)語(yǔ)言,我們就必需要了解C++里面的那些東西,需要我們?nèi)チ私馑竺娴膬?nèi)存對(duì)象。這樣我們才能真正的了解C++,從而能夠更好的使用C++這門(mén)最難的編程語(yǔ)言。
在文章束之前還是介紹一下自己吧。我從事軟件研發(fā)有十個(gè)年頭了,目前是軟件開(kāi)發(fā)技術(shù)主管,技術(shù)方面,主攻Unix/C/C++,比較喜歡網(wǎng)絡(luò)上的技術(shù),比如分布式計(jì)算,網(wǎng)格計(jì)算,P2P,Ajax等一切和互聯(lián)網(wǎng)相關(guān)的東西。管理方面比較擅長(zhǎng)于團(tuán)隊(duì)建設(shè),技術(shù)趨勢(shì)分析,項(xiàng)目管理。歡迎大家和我交流,我的MSN和Email是:haoel@hotmail.com