My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
ExerciseABCDReloadUnloadTestWithOOC  

Featured, Phase-Support
Updated Jul 24, 2012 by pin...@gmail.com

#多层继承关系的构造与析构的演示

多层继承关系的构造与析构的演示

通过逐层派生类演示构造与析构的顺序,方便OOC-GCC内存模型的理解

具体实现

#include "OOStd.h"

CLASS(A){
	int a;
	STATIC(A);
	int aa;
};
static int A_reload(A *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int A_unload(A *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int A_reloadSt(StA *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int A_unloadSt(StA *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
ASM(A,A_reload,A_unload,A_reloadSt,A_unloadSt)


CLASS_EX(B,A){
	int b;
	STATIC_EX(B,A);
	int bb;
};
static int B_reload(B *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int B_unload(B *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int B_reloadSt(StB *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int B_unloadSt(StB *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
ASM_EX(B,A,B_reload,B_unload,B_reloadSt,B_unloadSt)

CLASS_EX(C,B){
	STATIC_EX(C,B);
};
static int C_reload(C *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int C_unload(C *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int C_reloadSt(StC *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int C_unloadSt(StC *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
ASM_EX(C,B,C_reload,C_unload,C_reloadSt,C_unloadSt)


CLASS_EX(D,C){
	STATIC_EX(D,C);
};
static int D_reload(D *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int D_unload(D *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int D_reloadSt(StD *THIS,char *name){
	printf("%s\n",__FUNCTION__);
	return 0;
}
static int D_unloadSt(StD *THIS,void *PARAM){
	printf("%s\n",__FUNCTION__);
	return 0;
}
ASM_EX(D,C,D_reload,D_unload,D_reloadSt,D_unloadSt)

int main(){
	A *a;B *b;C *c;D *d;
	printf("\n*****************SIZE TEST\n");
	printf( "[A]size :%d static size:%d\n"
			"[B]size :%d static size:%d\n"
			"[C]size :%d static size:%d\n"
			"[D]size :%d static size:%d\n",
			sizeof(A),sizeof(StA),
			sizeof(B),sizeof(StB),
			sizeof(C),sizeof(StC),
			sizeof(D),sizeof(StD));
	printf("\n*****************NEW A\n");
	a=NEW0(A);
	printf("\n*****************NEW B\n");
	b=NEW0(B);
	printf("\n*****************NEW C\n");
	c=NEW0(C);

	printf("\n*****************DEL C\n");
	DELETE0(c);
	printf("\n*****************DEL B\n");
	DELETE0(b);
	printf("\n*****************DEL A\n");
	DELETE0(a);

	printf("\n*****************NEW D\n");
	d=NEW0(D);
	printf("\n*****************DEL D\n");
	DELETE0(d);

	printf("\n*****************NEW C DIFFERENT SEQUENCE\n");
	c=NEW0(C);
	printf("\n*****************NEW B DIFFERENT SEQUENCE\n");
	b=NEW0(B);
	printf("\n*****************NEW A DIFFERENT SEQUENCE\n");
	a=NEW0(A);
	printf("\n*****************DEL A DIFFERENT SEQUENCE\n");
	DELETE0(a);
	printf("\n*****************DEL B DIFFERENT SEQUENCE\n");
	DELETE0(b);
	printf("\n*****************DEL C DIFFERENT SEQUENCE\n");
	DELETE0(c);
	return 0;
}

正确输出

*****************SIZE TEST
[A]size :12 static size:12
[B]size :20 static size:16
[C]size :24 static size:16
[D]size :28 static size:16

*****************NEW A
A_reloadSt
A_reload

*****************NEW B
A_reloadSt
B_reloadSt
A_reload
B_reload

*****************NEW C
A_reloadSt
B_reloadSt
C_reloadSt
A_reload
B_reload
C_reload

*****************DEL C
C_unload
B_unload
A_unload
C_unloadSt
B_unloadSt
A_unloadSt

*****************DEL B
B_unload
A_unload
B_unloadSt
A_unloadSt

*****************DEL A
A_unload
A_unloadSt

*****************NEW D
A_reloadSt
B_reloadSt
C_reloadSt
D_reloadSt
A_reloadSt
B_reloadSt
C_reloadSt
A_reloadSt
B_reloadSt
A_reloadSt
A_reload
B_reload
C_reload
D_reload

*****************DEL D
D_unload
C_unload
B_unload
A_unload
A_unloadSt
B_unloadSt
A_unloadSt
C_unloadSt
B_unloadSt
A_unloadSt
D_unloadSt
C_unloadSt
B_unloadSt
A_unloadSt

*****************NEW C DIFFERENT SEQUENCE
A_reloadSt
B_reloadSt
C_reloadSt
A_reloadSt
B_reloadSt
A_reloadSt
A_reload
B_reload
C_reload

*****************NEW B DIFFERENT SEQUENCE
A_reload
B_reload

*****************NEW A DIFFERENT SEQUENCE
A_reload

*****************DEL A DIFFERENT SEQUENCE
A_unload

*****************DEL B DIFFERENT SEQUENCE
B_unload
A_unload

*****************DEL C DIFFERENT SEQUENCE
C_unload
B_unload
A_unload
A_unloadSt
B_unloadSt
A_unloadSt
C_unloadSt
B_unloadSt
A_unloadSt


Time Elapsed : 0.016000s
Powered by Google Project Hosting