zoukankan      html  css  js  c++  java
  • 用C结构体来实现面向对象编程,ti xDAIS标准算法就这么搞的(1)

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

    测试代码如下:

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

    此代码在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 ",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 ",(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.. ");
    }

    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.. ");
    }
    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(" ********************************** ");
    //第二种调用,注意结果
    Alg_process(Handle);//Handle
    Alg_control(Handle);
    //free(Handle);
    return 0;

    }

  • 相关阅读:
    网络七层模型
    border-radius 50% 和100%
    数据绑定
    前端一些基础的重要的知识2
    用 ul 和 li 模拟select控件
    两列布局
    盒子的水平垂直居中几种方法
    TCP时间戳
    帧聚合
    skb buff数据结构
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3486600.html
Copyright © 2011-2022 走看看