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

时间:2023-03-09 07:36:16
用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)

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

测试代码如下:

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

此代码在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;

}