找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 5192|回复: 3

【C】用Struct模拟C++里最简单的Class

[复制链接]

55

主题

275

回帖

9352

积分

用户组: 管理员

UID
77
精华
16
威望
237 点
宅币
8217 个
贡献
251 次
宅之契约
0 份
在线时间
254 小时
注册时间
2014-2-22
发表于 2016-1-4 16:05:21 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
Struct和Class最大的区别,在于Struct里只有数据,而Class里有函数。如果利用上“函数指针”这玩意,则可以用Struct模拟出最简单的Class。
下面是两个的例子,达到同样的目的:输入数字A和B,分别获得他们加减乘除的结果。第一个例子是C++的,第二个例子是C的。
  1. #include <stdio.h>
  2. #include <Windows.h>

  3. class CCALC
  4. {
  5. public:
  6.         int a;
  7.         int b;
  8.         int getAdd();
  9.         int getSub();
  10.         int getMul();
  11.         int getDiv();
  12.         CCALC::CCALC();
  13.         CCALC::~CCALC();
  14. //
  15. private:
  16.         //
  17. };

  18. CCALC::CCALC()
  19. {
  20.         ;//init
  21. }

  22. CCALC::~CCALC()
  23. {
  24.         ;//uninit
  25. }

  26. int CCALC::getAdd()
  27. {
  28.         return a+b;
  29. }

  30. int CCALC::getSub()
  31. {
  32.         return a-b;
  33. }

  34. int CCALC::getMul()
  35. {
  36.         return a*b;
  37. }

  38. int CCALC::getDiv()
  39. {
  40.         return a/b;
  41. }

  42. int main()
  43. {
  44.         CCALC CCalc;
  45.         CCalc.a=16;
  46.         CCalc.b=4;
  47.         printf("%ld\n%ld\n%ld\n%ld\n",CCalc.getAdd(),
  48.                                                                         CCalc.getSub(),
  49.                                                                         CCalc.getMul(),
  50.                                                                         CCalc.getDiv());
  51.         system("pause");return 0;
  52. }
复制代码
  1. #include <stdio.h>
  2. #include <Windows.h>

  3. typedef struct _CCALC
  4. {
  5.         int a;
  6.         int b;
  7.         int (*getAdd)(PCCALC);
  8.         int (*getSub)(PCCALC);
  9.         int (*getMul)(PCCALC);
  10.         int (*getDiv)(PCCALC);
  11. }CCALC, *PCCALC;

  12. static int CCALC__getAdd(PCCALC p)
  13. {
  14.         return p->a + p->b;
  15. }

  16. static int CCALC__getSub(PCCALC p)
  17. {
  18.         return p->a - p->b;
  19. }

  20. static int CCALC__getMul(PCCALC p)
  21. {
  22.         return p->a * p->b;
  23. }

  24. static int CCALC__getDiv(PCCALC p)
  25. {
  26.         return p->a / p->b;
  27. }

  28. PCCALC new_CCALC()
  29. {
  30.         PCCALC ret= (PCCALC)malloc(sizeof(CCALC));
  31.         ret->getAdd = CCALC__getAdd;
  32.         ret->getSub = CCALC__getSub;
  33.         ret->getMul = CCALC__getMul;
  34.         ret->getDiv = CCALC__getDiv;
  35.         return ret;
  36. }

  37. void delete_CCALC(PVOID p)
  38. {
  39.         free(p);
  40. }

  41. int main()
  42. {
  43.         PCCALC pCCalc = new_CCALC();
  44.         pCCalc->a=16;
  45.         pCCalc->b=4;
  46.         printf("%ld\n%ld\n%ld\n%ld\n", pCCalc->getAdd(pCCalc),
  47.                                                                         pCCalc->getSub(pCCalc),
  48.                                                                         pCCalc->getMul(pCCalc),
  49.                                                                         pCCalc->getDiv(pCCalc));
  50.         delete_CCALC(pCCalc);
  51.         system("pause");return 0;
  52. }
复制代码
很巧合的是,代码都是60行。

本帖被以下淘专辑推荐:

回复

使用道具 举报

1110

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24225 个
贡献
46222 次
宅之契约
0 份
在线时间
2296 小时
注册时间
2014-1-26
发表于 2016-1-4 16:17:15 | 显示全部楼层
内容简洁明了,不错。

其实函数指针出现在结构体里,作用类似于C艹的虚函数。
而虚函数的一个重要特性就是,它可以在不需要修改调用者行为的情况下,改变这个类/结构体的某个函数的行为。典型的就是封装了send和recv的网络套接字相关类,可以通过改变send和recv的行为来将其转换为支持SSL加密的网络套接字类。
回复 赞! 靠!

使用道具 举报

85

主题

175

回帖

3990

积分

用户组: 超级版主

No. 418

UID
418
精华
14
威望
53 点
宅币
1974 个
贡献
1582 次
宅之契约
0 份
在线时间
252 小时
注册时间
2014-8-9
发表于 2016-1-5 02:24:19 | 显示全部楼层
现在事情太多,无奈没有继续完成这篇文章。
不知@美俪女神 是否可以助我一臂之力?
http://www.0xaa55.com/forum.php?mod=viewthread&tid=1584
In the beginning I was not the best.
And the world was also not the best.
But I still know that I am who I am.
Because I think that it is good.
I have been working hard.
I have been keeping growth with the world.
And it was so.
回复 赞! 靠!

使用道具 举报

85

主题

175

回帖

3990

积分

用户组: 超级版主

No. 418

UID
418
精华
14
威望
53 点
宅币
1974 个
贡献
1582 次
宅之契约
0 份
在线时间
252 小时
注册时间
2014-8-9
发表于 2016-1-5 02:27:55 | 显示全部楼层
大概在//代码2-2里边就是这样一个实例
In the beginning I was not the best.
And the world was also not the best.
But I still know that I am who I am.
Because I think that it is good.
I have been working hard.
I have been keeping growth with the world.
And it was so.
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-19 11:07 , Processed in 0.053226 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表