用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)
用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的。
测试代码如下:
- #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=%d\n",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=%d\n",(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..
- */
此代码在ubuntu下运行还是有点问题出现两个类似的段错误,估计是野指针和未分配内存的缘故!
第一个解决:
ret->handle=(Alg_Handle)malloc(sizeof(Alg_Handle));
第二个:
我干脆把改了之后的完整代码贴上吧!
#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;
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=%d\n",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=%d\n",(a+b+c)*2);
}
void Alg_process(Alg_Handle handle)
{
struct triangle *ret= (struct triangle *)malloc(sizeof(struct triangle));
ret->handle=(Alg_Handle)malloc(sizeof(Alg_Handle));
ret->handle->fxns=(struct Alg_Fxn*)malloc(sizeof(struct Alg_Fxn));
ret->handle->fxns->process=(Alg_Handle *)malloc(sizeof(Alg_Handle));
ret->handle->fxns->process=tri_process;
ret=handle;
ret->handle->fxns->process(ret);
printf("in Alg_process.. \n");
}
void Alg_control(Alg_Handle handle)
{
struct triangle *ret= (struct triangle *)malloc(sizeof(struct triangle));
ret->handle=(Alg_Handle)malloc(sizeof(Alg_Handle));
ret->handle->fxns=(struct Alg_Fxn*)malloc(sizeof(struct Alg_Fxn));
ret->handle->fxns->control=(Alg_Handle *)malloc(sizeof(Alg_Handle));
ret->handle->fxns->control=tri_control;
ret->handle->fxns->control(ret);
//handle->fxns->control(handle);
printf("in Alg_control.. \n");
}
Alg_Fxn gfxns = {
tri_process,
tri_control,
};
int main()
{
struct triangle *ret= (struct triangle *)malloc(sizeof(struct triangle));
ret->a = 2;
ret->b = 3;
ret->c = 4;
ret->handle=(Alg_Handle)malloc(sizeof(Alg_Handle));
ret->handle->fxns=&gfxns;
Alg_Handle Handle= ret;
//第一种调用,注意结果
gfxns.process(Handle);
gfxns.control(Handle);
printf("\n**********************************\n");
//第二种调用,注意结果
Alg_process(Handle);//Handle
Alg_control(Handle);
//free(Handle);
return 0;
}
用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)的更多相关文章
- C语言:使用结构体和指针函数实现面向对象思想(OO编程)
原文:https://www.linuxidc.com/Linux/2016-12/138789.htm 有关:<C语言:过年回家 发现只有我没有对象> 一.基础研究 观察如下两个程序a. ...
- c语言里用结构体和指针函数实现面向对象思想
一.基础研究 观察如下两个程序a.c和b.c: A.c: B.c: 这两个程序都是要实现在屏幕上第10行40列打印一个绿色的字符c: 这两个程序的数据组织方式是一样的,都是使用结构体,而且对共性和个性 ...
- 【C/C++编程入门学习】C语言结构体硬核玩法分享,一切皆是数据!
前言 对于结构体的应用太多了,今天这篇文章我主要为大家总结平时关于结构体的一些独特硬核小技巧,对于结构体更多优秀的编程表现,如果你对结构体的基础知识还不具备的话得回头看一下专栏教程或者自己找本书籍学习 ...
- swift 的枚举、结构体、类
一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...
- Go语言中结构体的使用-第2部分OOP
1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...
- iOS学习笔记44-Swift(四)枚举和结构体
一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...
- Swift结构体与类
在面向过程的编程语言(如C语言)中,结构体用得比较多,但是面向对象之后,如在C++和Objective-C中,结构体已经很少使用了.这是因为结构体能够做的事情,类完全可以取而代之.而Swift语言却非 ...
- Java基础--面向对象编程1(类与对象)
1.类(class)的定义 类是对一组具有相同特征和行为的对象的抽象描述. 在程序中,引入类的概念,就是为了快速生成更多的具有相同特性和行为的事物. 2.对象(object)的定义 对象是类的具体实现 ...
- Go第六篇之结构体剖析
Go 语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go 语言中的类型可以被实例化,使用new或&a ...
随机推荐
- Oracle 11gR2数据库使用
1很奇怪,不太懂原理 一.Oracle 12c创建用户是出现“ORA-65096: invalid common user or role name”的错误 - CalvinR http://www. ...
- Maven install 报错: Failed to execute goalorg.apache.maven.plugins:maven-gpg-plugin:1.4:sign (sign-art
执行 Maven install 时报错: Failed to execute goalorg.apache.maven.plugins:maven-gpg-plugin:1.4:sign (sign ...
- unity, 人物与移动跳台的交互
----更新(2015-7-1): 也可以用itween来移动跳台. ---- 例如人物跳到往复运动的移动跳台上后应随跳台运动. 要实现这个效果有两个思路,一是用运动学一是用动力学. 用动力学实现就是 ...
- unity 显示帧率
Game视图右上角Stats按钮按下即可显示统计信息.
- OpenVpn的ipp.txt文件
ipp=ip pool我猜得,呵呵 ipp.txt文件中存放上一次连接时,客户端分配的ip地址. ipp.txt用来保存上次的连接状态的,并不能为客户端设置固定ip地址. 该文件的格式为:用户名,ip ...
- 浅谈CPU,GPU,TPU,DPU,NPU,BPU
https://www.sohu.com/a/191538165_777155 A12宣传的每秒5万亿次运算,用计算机语言描述就是5Tops. 麒麟970 NPU,根据资料是 1.92Tops. 麒麟 ...
- C#compiler
http://www.cnblogs.com/Ninputer/archive/2011/06/12/2078671.html Compilers - Managed Profile-Guided O ...
- ubuntu 清理系统垃圾与备份
虽然linux下不会有windows下的那么多垃圾和磁盘碎片!但还是会留下一些用不着的临时文件或是多次升级后的N个旧的内核! 1,非常有用的清理命令:sudo apt-get autocleansud ...
- error LNK2019: 无法解析的外部符号(编程解决方法)
正在编译...1>Ipv4IPv6traceroutesrc.cpp1>d:\研究生\c++\study\test\test\ipv4ipv6traceroutesrc.cpp(461) ...
- shell学习笔记之变量(一)
一.普通变量 1.使用变量之前通常并不需要事先声明,通常赋值的时候创建他们2.默认所有的变量都被看做字符串,并且以字符串存储3.变量区分大小写4.变量名前面添加$符号来访问变量,赋值的时候只需要使用变 ...