用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 ...
随机推荐
- ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17) 介绍
阅读文章:<ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17)> 这篇文章是对一项中文检测和识别比赛项目( ...
- Qt解析xml
发现用 Qt 解析 xml 文件非常方便,下面是一个简单的解析 xml 文件的例子: #include <QtCore/QCoreApplication> #include <QDo ...
- Common Probability Distributions
Common Probability Distributions Probability Distribution A probability distribution describes the p ...
- [na]交换技术知识点-提纲
vlan - trunk - vtp(vtp prune) stp portfast rootgurd bpduguard bpdufilter uplinkfast backbonfast loop ...
- Amoeba软件实现mysql读写分离
一般不用,大公司都是自己程序实现的. 安装amoeba
- 普通用户su 到root,无需密码方式,及iptables封掉本机某个端口,core文件配置
一. 普通用户su到root无需密码: 随着服务器越来越多,普通用户转到root下,去查密码表是个很繁琐的事,发现有如下方式比较方便(需要root操作) vi /etc/pam.d/su 将 aut ...
- Python 2.7.9 Demo - 003.01.只允许相同缩进
Right #!/usr/bin/python if True: print ("True"); print('Again'); else: print ("False& ...
- CAsyncSocket编程 MFC
许多时候我们实现网络编程使用的是winsock api函数,虽然这些函数使用起来也很方便,很灵活,但是VC++的MFC类库中提供了CAsyncSocket这样一个套接字类,用它来实现socket编程会 ...
- .balignl 16,0xdeadbeef浅析
http://zqwt.012.blog.163.com/blog/static/12044684201031102956976/ 最近在分析u-boot的源代码,看到这一行: .bal ...
- 【转】java中&和&&的区别和联系
[转]http://www.cnblogs.com/hongten/p/hongten_java_yu.html 电路问题总结: 对于:& -- > 不管怎样,都会执行" ...