用C语言模拟面向对象
来源:互联网 发布:优盘格式化恢复软件 编辑:程序博客网 时间:2024/06/06 01:20
这个代码是修改自lwoopc,截取其中一部分,然后添加一部分。
在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);//testprintf("delete:%d\n",h->a);//testprintf("\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);//testprintf("dtor:%c\n",e->c);printf("\n\n\n");}
- 用C语言模拟面向对象编程
- 用C语言模拟面向对象
- 用C语言模拟面向对象
- 用C语言模拟面向对象
- 用C语言模拟面向对象编程(转)
- 用C语言模拟面向对象编程(上)
- 用C语言模拟面向对象编程(上)
- 老码识途 创造面向对象语言(使用C模拟C++)
- C语言面向对象的模拟(1)
- C语言面向对象
- C语言面向对象
- C语言中的面向对象-C语言中的类模拟
- 用C语言进行面向对象编程
- 用C语言进行面向对象编程
- 用C语言进行面向对象编程
- 用C语言实现面向对象编程
- 用C语言实现面向对象
- 用 C 语言实现面向对象编程
- Please ensure that adb is correctly located 问题的解决
- const map的初始化及使用
- 一个替换某文件夹下的所有文件中特定字符串的小工具
- 观后感---Jdk的安装和java的核心机制
- 使用logback 替换log4j实例
- 用C语言模拟面向对象
- ETL入门基础与设计讲解
- 一条shell指令统计项目的代码行数
- 华为C8815 默认logcat信息
- LeetCode | 3Sum
- Qt中处理First chance exception异常
- HTTPS连接的前几毫秒发生了什么
- AutoCAD二次开发之视图操作(2)通过指定点设置视图。
- liferay ide创建主题,有时候就报错Exception in thread java.lang.StackOverflowError