我是靠谱客的博主 洁净巨人,这篇文章主要介绍用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的,现在分享给大家,希望可以做个参考。

用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的。

测试代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Alg_Obj{ struct Alg_Fxn* fxns; }Alg_Obj; typedef Alg_Obj *Alg_Handle; typedef struct Alg_Fxn{ void (*process)(Alg_Handle handle); void (*control)(Alg_Handle handle); }Alg_Fxn; void Alg_process(Alg_Handle handle) { handle->fxns->process(handle); printf("in Alg_process.. n"); } void Alg_control(Alg_Handle handle) { handle->fxns->control(handle); printf("in Alg_control.. n"); } struct triangle{ Alg_Handle handle; int a; int b; int c; }; void tri_process(Alg_Handle handle) { struct triangle *t = (struct triangle*)handle; int a = t->a; int b = t->b; int c = t->c; printf(" [in tri_process] sum=%dn",a+b+c); } void tri_control(Alg_Handle handle) { struct triangle *t = (struct triangle*)handle; int a = t->a; int b = t->b; int c = t->c; printf(" [in tri_control] sum=%dn",(a+b+c)*2); } Alg_Fxn gfxns = { tri_process, tri_control, }; int main() { struct triangle *ret = (struct triangle*)malloc(sizeof(struct triangle)); ret->handle->fxns=&gfxns; ret->a = 2; ret->b = 3; ret->c = 4; Alg_Handle Handle= (Alg_Handle)ret; //第一种调用,注意结果 gfxns.process(Handle); gfxns.control(Handle); printf("n**********************************n"); //第二种调用,注意结果 Alg_process(Handle); Alg_control(Handle); free(Handle); return 0; } /* [root@localhost TestCode]# ./a.out [in tri_process] sum=9 [in tri_control] sum=18 ********************************** [in tri_process] sum=9 in Alg_process.. [in tri_control] sum=18 in Alg_control.. */


最后

以上就是洁净巨人最近收集整理的关于用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的的全部内容,更多相关用C结构体来实现面向对象编程,ti内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部