这个例子主要用来掩饰如何实现继承关系[v0.6RC1]
http://code.google.com/p/ooc-gcc/downloads/list
// #include "inc/core/OOStd.h" CLASS(Human){ int age; int gender; STATIC(Human) iFn sayHi; }; ASM(Human,NULL,NULL,NULL,NULL) CLASS_EX(Human,Mom){ STATIC_EX(Human,Mom) }; int Mom_sayHi(Mom * THIS){ printf("Hi,Mom age:%d gender:%s\n",THIS->age,THIS->gender?"male":"female"); return 0; } int Mom_reload(Mom * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); THIS->age=36; THIS->gender=0; return 0; } int Mom_unload(Mom * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); return 0; } int Mom_reloadStatic(StaticMom * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); THIS->sayHi=(void *)Mom_sayHi; return 0; } int Mom_unloadStatic(StaticMom * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); THIS->sayHi=NULL; return 0; } ASM_EX(Human,Mom,Mom_reload,Mom_unload,Mom_reloadStatic,Mom_unloadStatic) CLASS_EX(Mom,Kid){ int id; STATIC_EX(Mom,Kid) }; int Kid_sayHi(Kid * THIS){ printf("Hi,Kid%d age:%d gender:%s\n",THIS->id,THIS->age,THIS->gender?"male":"female"); return 0; } int Kid_reload(Kid * THIS,void *PARAM){ static int id=0; printf("%s %p\n",__func__,THIS); THIS->age=3; THIS->gender=1; THIS->id=++id; return 0; } int Kid_unload(Kid * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); return 0; } int Kid_reloadStatic(StaticKid * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); THIS->sayHi=(void *)Kid_sayHi; return 0; } int Kid_unloadStatic(StaticKid * THIS,void *PARAM){ printf("%s %p\n",__func__,THIS); THIS->sayHi=NULL; return 0; } ASM_EX(Mom,Kid,Kid_reload,Kid_unload,Kid_reloadStatic,Kid_unloadStatic) int main(){ printf("\n-----------\n"); Mom * m=newMom(NULL); printf("\n-----------\n"); StaticMom *stm=ST(Mom,m); Kid *k1=newKid(NULL); printf("\n-----------\n"); Kid *k2=newKid(NULL); printf("\n-----------\n"); Kid *k3=newKid(NULL); printf("\n-----------\n"); stm->sayHi(m); printf("\n-----------\n"); StaticKid *stk=ST(Kid,k1); stk->sayHi(k1); printf("\n-----------\n"); stk->sayHi(k2); printf("\n-----------\n"); stk->sayHi(k3); printf("\n-----------\n"); delKid(&k1,NULL); printf("\n-----------\n"); delKid(&k2,NULL); printf("\n-----------\n"); delKid(&k3,NULL); printf("\n-----------\n"); delMom(&m,NULL); printf("\n-----------\n"); return 0; }
正确的输出为,
----------- Mom_reloadStatic 003E2BF0 Mom_reload 003E2BD0 ----------- Mom_reload 003E2C18 Mom_reloadStatic 003E2C40 Kid_reloadStatic 003E2C40 Kid_reload 003E2C18 ----------- Mom_reload 003E2C80 Kid_reload 003E2C80 ----------- Mom_reload 003E2CA8 Kid_reload 003E2CA8 ----------- Hi,Mom age:36 gender:female ----------- Hi,Kid1 age:3 gender:male ----------- Hi,Kid2 age:3 gender:male ----------- Hi,Kid3 age:3 gender:male ----------- Kid_unload 003E2C18 Mom_unload 003E2C18 ----------- Kid_unload 003E2C80 Mom_unload 003E2C80 ----------- Kid_unload 003E2CA8 Kid_unloadStatic 003E2C40 Mom_unloadStatic 003E2C40 Mom_unload 003E2CA8 ----------- Mom_unload 003E2BD0 Mom_unloadStatic 003E2BF0 ----------- Time Elapsed Seconds : 0 Microseconds : 0
另外内存分配检测Log输出为
[,][;] +,0x003E2BD0,20; +,0x003E4CF0,8; +,0x003E2BF0,32; +,0x003E2C18,28; +,0x003E2C40,52; +,0x003E2C80,28; +,0x003E2CA8,28; -,0x003E2C18; -,0x003E2C80; -,0x003E2C40; -,0x003E2CA8; -,0x003E2BF0; -,0x003E4CF0; -,0x003E2BD0; !,0;
最后的0表示分配的都释放了,而前面的数据可以配合graphviz来实现一些图表
具体文件见项目地址,我一直比较懒,懒得用svn,git这些工具,因为这个目前依旧是自己自娱自乐
P.S.
注意编译时要开启-fms-extensions[GCC 4.5]或-fplan9-extensions[GCC 4.6]