目录
1.前言
版权所有,请勿转载。
本篇文章的所有代码例子,如果是windows上编译运行,则使用的是visual studio 2013。如果是RHEL6.5平台(linux kernal: 2.6.32-431.el6.i686)上编译运行,则其gcc版本为4.4.7,如下所示:
[root@MiWiFi-R1CM ~]# gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
2.菱形继承类的内存分布
2.1.类的结构
菱形继承 - 重复继承
2.2.实现代码windows版本
下面的代码运行在windows 7+visual studio 2013平台上。输出Derive类的内存分布。
/*
版权所有.
作者: 水草(shltsh@hotmail.com)
*/
#include <iostream>
using namespace std;
class Base {
public:
int _iBase;
char _cBase;
public:
Base() : _iBase(1111), _cBase('A') {}
virtual void func() {
cout << "Base::func()" << endl;
}
virtual void baseFunc() {
cout << "Base::baseFunc()" << endl;
}
};
class Base1 : public Base {
public:
int _iBase1;
char _cBase1;
public:
Base1() : _iBase1(2222), _cBase1('B') {}
virtual void func() {
cout << "Base1::func()" << endl;
}
virtual void func1() {
cout << "Base1::func1()" << endl;
}
virtual void baseFunc1() {
cout << "Base1::baseFunc1()" << endl;
}
};
class Base2 : public Base {
public:
int _iBase2;
char _cBase2;
public:
Base2() : _iBase2(3333), _cBase2('C') {}
virtual void func() {
cout << "Base2::func()" << endl;
}
virtual void func2() {
cout << "Base2::func2()" << endl;
}
virtual void baseFunc2() {
cout << "Base2::baseFunc2()" << endl;
}
};
class Derive : public Base1, public Base2 {
public:
int _iDerive;
char _cDerive;
public:
Derive() : _iDerive(4444), _cDerive('D') {}
virtual void func() {
cout << "Derive::func()" << endl;
}
virtual void func1() {
cout << "Derive::func1()" << endl;
}
virtual void func2() {
cout << "Derive::func2()" << endl;
}
virtual void deriveFunc() {
cout << "Derive::deriveFunc()" << endl;
}
};
int main() {
typedef void(*Fun)(void);
int** pVtab = NULL;
Fun pFun = NULL;
Derive derive;
pVtab = (int**)&derive;
cout << "[0] Derive::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] ";
pFun();
pFun = (Fun)pVtab[0][5];
cout << " [5] 0x" << pFun << endl;
cout << "[1] Base::_iBase = " << (int)pVtab[1] << endl;
cout << "[2] Base::_cBase = " << (char)(int)pVtab[2] << endl;
cout << "[3] Base1::_iBase1 = " << (int)pVtab[3] << endl;
cout << "[4] Base1::_cBase1 = " << (char)(int)pVtab[4] << endl;
cout << "[5] Derive::Base2::_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] Base::_iBase = " << (int)pVtab[6] << endl;
cout << "[7] Base::_cBase = " << (char)(int)pVtab[7] << endl;
cout << "[8] Base2::_iBase2 = " << (int)pVtab[8] << endl;
cout << "[9] Base2::_cBase2 = " << (char)(int)pVtab[9] << endl;
cout << "[10] Derive::_iDerive = " << (int)pVtab[10] << endl;
cout << "[11] Derive::_cDerive = " << (char)(int)pVtab[11] << endl;
return 0;
}
运行结果如下:
2.3.实现代码linux版本
linux平台下的代码,与上一节中的windows代码,绝大部分是相似的,不同点仅在于linux版本中,会访问并打印出pVtab[0][6]的值。
下面给出了完整的main函数。
/*
版权所有.
作者: 水草(shltsh@hotmail.com)
*/
int main() {
typedef void(*Fun)(void);
int** pVtab = NULL;
Fun pFun = NULL;
Derive derive;
pVtab = (int**)&derive;
cout << "[0] Derive::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] ";
pFun();
pFun = (Fun)pVtab[0][5];
cout << " [5] ";
pFun();
pFun = (Fun)pVtab[0][6];
cout << " [6] 0x" << pFun << endl;
cout << "[1] Base::_iBase = " << (int)pVtab[1] << endl;
cout << "[2] Base::_cBase = " << (char)(int)pVtab[2] << endl;
cout << "[3] Base1::_iBase1 = " << (int)pVtab[3] << endl;
cout << "[4] Base1::_cBase1 = " << (char)(int)pVtab[4] << endl;
cout << "[5] Derive::Base2::_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] Base::_iBase = " << (int)pVtab[6] << endl;
cout << "[7] Base::_cBase = " << (char)(int)pVtab[7] << endl;
cout << "[8] Base2::_iBase2 = " << (int)pVtab[8] << endl;
cout << "[9] Base2::_cBase2 = " << (char)(int)pVtab[9] << endl;
cout << "[10] Derive::_iDerive = " << (int)pVtab[10] << endl;
cout << "[11] Derive::_cDerive = " << (char)(int)pVtab[11] << endl;
return 0;
}
运行结果如下:
2.4.Derive内存分布对比
通过下面的对比图,可以看到,对于菱形非虚继承,Derive类在这两种平台上的内存分布几乎一样。
Windows 7 + Visual studio 2013 Linux RHEL6.5 + gcc 4.4.7