1. Preface
For all the code examples in this article, if you are running on Windows compilation, you are using Visual Studio 2013. If the RHEL6.5 platform (Linux kernal:2.6.32-431.el6.i686) is compiled and run, its GCC version is 4.4.7 as follows:
[email protected] ~]# gcc--version
GCC (gcc) 4.4.7 20120313 (Red Hat 4.4.7-4)2. Memory distribution of a diamond-inheriting class2.1. Structure of the class
Diamond Inheritance-Repeating inheritance
2.2. Implementing the Code Windows version
The following code runs on the Windows 7+visual Studio 2013 platform.
#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 fun C () {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 voi D 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::d erivefunc ()" << 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] ';p fun ();p fun = (fun) pvtab[0][1];cout << "[1]";p fun ();p fun = ( Fun) pvtab[0][2];cout << ' [2] ';p fun ();p fun = (fun) pvtab[0][3];cout << "[3]";p fun ();p fun = (fun) Pvtab[0][4];cout << "[4]";p fun ();p fun = (fun) pvtab[0][5];cout << "[5] 0x" << pfun << ; Endl;cout << "[1] base::_ibase =" << (int) pvtab[1] << endl;cout << "[2] base::_cbase =" <&L T (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] ';p fun ();p fun = (fun) pvtab[5][1];cout << "[1]";p fun ();p fun = (Fu N) pvtab[5][2];cout << "[2]";p fun ();p fun = (fun) pvtab[5][3];cout << "[3]";p fun ();p fun = (fun) PV Tab[5][4];cout << "[4] 0x" << pfun << endl;cout << "[6] base::_ibase =" << (int) pVt AB[6] << endl;cout << "[7] base::_cbase =" << (char) (int) pvtab[7] << endl;cout << "[8] Ba Se2::_ibase2 = "<< (int) pvtab[8] << endl;cout <<" [9] Base2::_cbase2 = "<< (char) (int) pvtab[9] << endl;cout << [ten] derive::_iderive = "<< (int) pvtab[10] << endl;cout <<" [One] Derive::_ cderive = "<< (chAR) (int) pvtab[11] << Endl;return 0;} The results of the operation are as follows:
2.3. Implementing the Code Linux version
The code under the Linux platform, which is mostly similar to the Windows code in the previous section, differs only in the Linux version, and the value of pvtab[0][6] is accessed and printed. The complete main function is given below.
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 << "[ten] derive::_iderive =" << (int) pvtab[10] << Endl; cout << "[one] derive::_cderive =" << (char) (int) pvtab[11] << Endl; return 0;}The results of the operation are as follows:
2.4. Memory Distribution comparison
From the comparison chart below, you can see that for diamond non-virtual inheritance, the memory distribution on both platforms is almost the same.
Windows 7 + Visual Studio Linux RHEL6.5 + gcc 4.4.7
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Object Memory Distribution (2)-Diamond Inheritance (non virtual)