C++ 对象的内存布局(下)

C++ 对象的内存布局(下)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

陈皓

http://blog.csdn.net/haoel

<<<点击这里查看上篇

重复继承

下面我们再来看看,发生重复继承的情况。所谓重复继承,也就是某个基类被间接地重复继承了多次。

下图是一个继承图,我们重载了父类的f()函数。

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 223.5pt; HEIGHT: 294pt" o:ole="" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CHAO~1.CHE%5CLOCALS~1%5CTemp%5Cmsohtml1%5C13%5Cclip_image001.emz"></imagedata></shape>

C++ 对象的内存布局(下)

其类继承的源代码如下所示。其中,每个类都有两个变量,一个是整形(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++ 2003G++ 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

下面是对于子类实例中的虚函数表的图:

<shape id="_x0000_i1027" style="WIDTH: 315pt; HEIGHT: 240pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CHAO~1.CHE%5CLOCALS~1%5CTemp%5Cmsohtml1%5C13%5Cclip_image003.png"><font color="#000000" size="3"></font></imagedata></shape>

C++ 对象的内存布局(下)

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

D d;

d.ib = 0;//二义性错误

d.B1::ib = 1; //正确

d.B2::ib = 2; //正确


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

钻石型多重虚拟继承

虚拟继承的出现就是为了解决重复继承中多个间接父类的问题的。钻石型的结构是其最经典的结构。也是我们在这里要讨论的结构:

上述的“重复继承”只需要把B1B2继承B的语法中加上virtual 关键,就成了虚拟继承,其继承图如下所示:

<shape id="_x0000_i1026" style="WIDTH: 221.25pt; HEIGHT: 302.25pt" o:ole="" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CHAO~1.CHE%5CLOCALS~1%5CTemp%5Cmsohtml1%5C13%5Cclip_image005.emz"><font color="#000000" size="3"></font></imagedata></shape>

C++ 对象的内存布局(下)

上图和前面的“重复继承”中的类的内部数据和接口都是完全一样的,只是我们采用了虚拟继承:其省略后的源码如下所示:

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) <<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 << color: ol

你可能感兴趣的:(C++,c,C#,F#,vc++)