用C语言模拟面向对象
2013-09-22 14:15
232 查看
这个代码是修改自lwoopc,截取其中一部分,然后添加一部分。
在VC6.0和gcc可编译,大家可以从程序代码中的"//文件开始部分"开始看
CTOR是构造,DTOR是析构
在VC6.0和gcc可编译,大家可以从程序代码中的"//文件开始部分"开始看
CTOR是构造,DTOR是析构
#include <stdio.h> #include <malloc.h> #define CLASS(type) \ typedef struct type type; \ type* type##_new( ); \ void type##_ctor(type* t); \ void type##_dtor(type* t); \ void type##_delete(type* t); \ struct type #define CTOR(type,sencentes) \ type* type##_new() { \ struct type *cthis; \ cthis = (struct type*)malloc(sizeof(struct type)); \ if(!cthis) \ { \ return 0; \ } \ type##_ctor(cthis); \ return cthis; \ } \ \ void type##_ctor(type* cthis) {sencentes;} #define DTOR(type,sencentes) \ void type##_delete(type* cthis) \ { \ type##_dtor(cthis); \ free(cthis); \ } \ void type##_dtor(type* cthis){sencentes;} #define SET(name,value) ( cthis -> name ) = (value) #define FUN(retType,funName) retType(* funName) #define CONNECT(funName) ( cthis -> funName ) = (& _##funName) #define INCALL(funName) (* cthis->funName) //在成员函数内调用其他函数 #define CALL(this,funName) (* this->funName) //文件开始部分 void _coutSome(char * str) //在原来函数名前加上_,和CONNECT宏相关 { printf("%s\n",str); } //haha 类 CLASS(haha){ //声明type##_new()和type##_ctor() 和 type##_delete()和type##_dtor() 函数 #define MEMhaha int a; \ int b; FUN(void,coutSome)(); //没定义在MEMhaha里面,表示自己专用的 MEMhaha }; CTOR(haha,SET(a,10); SET(b,5); CONNECT(coutSome)) //定义type##_new()和type##_ctor()函数 括号内第二个参数中的语句用;号隔开 DTOR(haha,SET(a,0); INCALL(coutSome) ("dtoring..."))//定义type##_delete()和type##_dtor()函数 括号内第二个参数中的语句用;号隔开 //wuwu 类,只有部分成员继承于haha,构造和析构等不继承,因为担心内存地址错误【继承后的排布不同】 CLASS(wuwu){ #define MEMwuwu MEMhaha \ char c; MEMwuwu }; CTOR(wuwu, printf("I am ctoring...\n"); SET(a,10); SET(c,'w') ) //语句用;号隔开 DTOR(wuwu, printf("I am dtoring...\n"); SET(a,0) ; SET(c,'?') ) //erer 类,和wuwu一模一样,但是本身只是把wuwu中的成员链接过来 CLASS(erer) { #define MEMerer MEMwuwu }; CTOR(erer, printf("I am ctoring...\n"); SET(a,10); SET(c,'w') ) //语句用;号隔开 DTOR(erer, printf("I am dtoring...\n"); SET(a,0) ; SET(c,'?') ) void main() { // 需要自己new和delete哦,C语言只能先定义后使用,所以下面几行必须放在最开始 wuwu *w; haha *h; erer *e; printf("\n\n\n"); h = haha_new(); CALL(h,coutSome)("newed"); printf("new:%d\n",h->a); printf("new:%d\n",h->b); h->a = 100; printf("set value:%d\n",h->a); haha_delete(h); //test printf("delete:%d\n",h->a); //test printf("\n\n\n"); w = wuwu_new(); //继承 printf("new:%d\n",w->a); printf("new:%c\n",w->c); w->a = 100; w->c = 'c'; printf("set value:%d\n",w->a); printf("set value:%c\n",w->c); wuwu_delete(w); //test,这个正常情况下不被调用 printf("dtor:%d\n",w->a); printf("dtor:%c\n",w->c); printf("\n\n\n"); e = erer_new(); printf("new:%d\n",e->a); printf("new:%c\n",e->c); e->a = 100; e->c = 'c'; printf("set value:%d\n",e->a); printf("set value:%c\n",e->c); erer_delete(e); printf("dtor:%d\n",e->a); //test printf("dtor:%c\n",e->c); printf("\n\n\n"); }
相关文章推荐
- 用C语言模拟面向对象
- C语言中的面向对象-C语言中的类模拟
- C语言中的面向对象-类模拟的性能分析
- C语言面向对象的模拟(1)
- C语言中的面向对象(1)-类模拟和多态,继承
- 用C语言模拟面向对象
- 【c语言】模拟实现strcat函数
- 用c语言.模拟实现strcpy,strcat,strcat,memcpy,memmove
- C语言实现面向对象
- 自己主动化測试程序之中的一个自己定义键盘的模拟測试程序(C语言)
- 软件设计师2007年11月下午试题5(C语言 面向对象)
- js 模拟面向对象的方式
- JS模拟面向对象全解(六、扩展方法)
- 36、C++对C语言的面向对象的扩充
- 【C语言】【面试题】【笔试题】模拟实现strncpy
- 用C语言实现面向对象的开发
- 面向对象的C语言开发框架:Nesty
- 用C语言模拟面向对象编程
- c语言下的通用数据库接口(之sqlite消化,模拟c#,java的反射)
- C语言数据结构实现银行模拟