子類記憶體分配
測試1:
#include
using namespace std;
typedef
void
(*func_t)
(void);
#define non_static_data_num 2
//繼承的和自己的
#define virtual_self_func_num 4
//繼承的虛函式與自己的任何函式總量
class father
virtual void
func1()
virtual void
func2()
virtual void
func3()
static
int z;
int x =
300;
int y =
400;};
class mother
virtual void
handle1()
void
handle2()
void
handle3()
public:
int m =
100;
int n =
200;};
class son :public mother, public father
void
son(
)int s =
500;};
intmain()
//通過物件位址訪問物件的三個資料成員位址和值(靜態資料成員儲存在類中,不能通過物件位址檢視)
for(
int i =
0; i < non_static_data_num; i++
) cout <<
"及值:"
<< dec <
int*)(
int(
&f)+4*
1)<<
" = "
<< f.m << endl;
cout <<
"及值:"
<< dec <
int*)(
int(
&f)+4*
2)<<
" = "
<< f.n << endl;
cout <<
"及值:"
<< dec <
int*)(
int(
&f)+4*
3)<<
" = "
<< f.x << endl;
cout <<
"及值:"
<< dec <
int*)(
int(
&f)+4*
4)<<
" = "
<< f.y << endl;
cout <<
"及值:"
<< dec <
int*)(
int(
&f)+4*
5)<<
" = "
<< f.s << endl;
//通過
system
("pause");
return0;
}
測試2:
#include
using namespace std;
class father
virtual void
func2()
virtual void
func3()
void
func4()
public:
//為了便於測試main函式調取資料成員,特別改用public
int x =
100;
int y =
200;
static
int z;};
int father:
:z =0;
class mother
void
handle2()
void
handle3()
};class son :public father, public mother
void
handle1()
virtual void
boy(
)int m =
400;
int n =
500;};
typedef
void
(*func_t)
(void);
intmain
(void
)for
(int i =
0; i <
2; i++
)int
* vptr3 =
(int*)
*((int*)
&son +3)
;for
(int i =
0; i <
1; i++
)for
(int i =
0; i <
2; i++
)return0;
}
final終結版
override強調重寫
#include
using namespace std;
class billgates final };
class dad };
class mom
virtual void
rules
() final };
class me :public dad, public mom
void
eat()}
;class daughter :public me };
void
party
(dad& dad, mom& mom, me& me)
intmain()
#include
using namespace std;
class shape
string getcolor()
virtual int
area()
=0;//僅虛函式允許純說明符,且純說明符僅允許=0
private:
string color;};
class circle:public shape
//相當於this->radius = radius;
intarea()
private:
int radius;};
intmain()
C 繼承,虛繼承 記憶體結構 詳解
目錄 class test1 private int num1 class test2 public test1 private int num2 void main test2記憶體結構 檢視記憶體發現父類在子類的上面 在原有的 基礎上增加了test3類 test3類繼承了 test2和test1...
C 虛繼承和虛繼承
虛繼承是在多繼承中為了解決衝突而技術。學術一點來說,是指乙個指定的基類,在繼承體系結構中,將其成員資料例項共享給也從這個基類直接或間接派生的其他類。虛繼承非常有用,可以避免多繼承的歧義和多重拷貝。考慮有如下繼承結構。b和c繼承a,d多繼承b c,我們看以下 class a class b publi...
c 虛繼承物件的記憶體布局
好了,我們從最基礎的的討論起。當c 支援virtual base class 時,就會多了一些額外負擔,當class 中內含乙個或多個virtual base class subobject時,將分成兩個部分,乙個不變區域性和乙個共享區域性。最初的方案是為每乙個虛基類安插乙個指標指向這個虛基類,其缺...