用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;
}