C结构体尽然可以这样用?

ghostyu 2012-08-28 10:06:49
下面是appro方案中关于白平衡一段的C代码,简化如下

typedef struct A_Obj{
struct A_Fxns *fxns;
}A_Obj;

typedef struct A_Obj *A_Handle;

typedef struct A_Fxns{
int a;
int (*process)(A_Handle handle,int b);
int (*control)(A_Handle handle,int b)
}A_Fxns;



感觉这个A_Handle绕了一圈又绕道自己肚子里去了,请大家看看这是怎么回事
...全文
208 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lubing521 2013-10-10
  • 打赏
  • 举报
回复
很好的一个帖子.不过此方法貌似不常用啊
ghostyu 2012-08-29
  • 打赏
  • 举报
回复
2楼 5楼说的对,这是ti的 xDAIS标准算法接口,只能用c来实现面向对象,结贴
我的测试代码如下:

#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..

*/

ghostyu 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

还可以吧,个人感觉不难懂,,,
[/Quote]

那这是什么个意思
jiangshi061 2012-08-28
  • 打赏
  • 举报
回复
结构体的互引用,

struct A_Fxns *fxns;//这是一个指针

int (*process)(A_Handle handle,int b);//这也是一个指针

这样就可以理解了,
比方说 A_Obj 实例化一个对象 a , a 中有一个A_Fxns 类型的指针,
那么系统不管这个指针指向哪里,都为其分配一个 4字节的指针(X86),因为指针的长度是不变的,所以你的声明和实例化都是合法的。
yaya_lucky 2012-08-28
  • 打赏
  • 举报
回复
还可以吧,个人感觉不难懂,,,
ies_sweet 2012-08-28
  • 打赏
  • 举报
回复
用C实现面向对象
太复杂了
感觉没有必要

有 objective-c
还有C++, JAVA
这个都是语言级别上就实现了面向对象
ghostyu 2012-08-28
  • 打赏
  • 举报
回复
上面代码运行平台是linux+DM368
BYD123 2012-08-28
  • 打赏
  • 举报
回复
这样的代码看起来好累。
如果要用面向对象的C,可以用G-Object/GLib库。
图灵狗 2012-08-28
  • 打赏
  • 举报
回复
这是用C语言来实现面向对象的写法,把A_Handle handle看成this指针就容易理解了。
sowhat_Ah 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
下面是appro方案中关于白平衡一段的C代码,简化如下
C/C++ code

typedef struct A_Obj{
struct A_Fxns *fxns;
}A_Obj;

typedef struct A_Obj *A_Handle;

typedef struct A_Fxns{
int a;
int (*process)(A_Handle handle,……
[/Quote]

好像是相互包含,就是不知道这些结构体具体怎么用的,出于什么用意

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧