简体   繁体   English

c中结构中的函数指针有什么用?

[英]What good is a function pointer inside a struct in c?

I guess using a function pointer inside a struct has something to do with encapsulating a function within a structure...? 我想在结构中使用函数指针与在结构中封装函数有关...? If so, then how exactly is this achieved?? 如果是这样,那究竟是如何实现的?

And what good does it bring to have a function pointer inside a struct rather than simply defining the function? 它在结构中有一个函数指针而不是简单地定义函数会带来什么好处呢?

function pointers inside structures are the base of object-programming in C (see http://www.planetpdf.com/codecuts/pdfs/ooc.pdf ). 结构中的函数指针是C中对象编程的基础(参见http://www.planetpdf.com/codecuts/pdfs/ooc.pdf )。 It is really really for medium-to-large C projects. 它确实适用于中到大型C项目。

An example: 一个例子:

Header: 标题:

typedef struct TPile
{
    int(*Push)(struct TPile*, int);
    int(*Pop)(struct TPile*);
    void(*Clear)(struct TPile*);
    void(*Free)(struct TPile*);
    int(*Length)(struct TPile*);
    void(*View)(struct TPile*);

    int Nombre;

    struct Titem *Top;

} TPile ;

Source: 资源:

TPile TPile_Create()
{
    TPile This;
    TPile_Init(&This);
    This.Free = TPile_Free;

    return This;
}


TPile* New_TPile()
{
    TPile *This = malloc(sizeof(TPile));
    if(!This) return NULL;
    TPile_Init(This);
    This->Free = TPile_New_Free;

    return This;
}


void TPile_Clear(TPile *This)
{
    Titem *tmp;

    while(This->Top)

    {
      tmp = This->Top->prec;
      free(This->Top);
      This->Top = tmp;
    }

    This->Nombre = 0;
}

Having function pointer inside struct will be useful for certain data structures such as binary search tree. 在struct中包含函数指针对于某些数据结构(如二叉搜索树)非常有用。

Lets say , i want to insert an element whose struct is 可以说,我想插入一个结构为的元素

struct Employee {
      int eid;
      char *name;
 };

into a binary search tree. 进入二叉搜索树。 but i would like the BST to use my function to compare the elements while storing and searching. 但我希望BST在存储和搜索时使用我的函数来比较元素。

and the bst struct will be as follows. 并且bst结构将如下。

 struct BST {
     struct _node *root;
     int (*compare)(void *e1 , void *e2);
 };

Now, i will use the BST as follows. 现在,我将按如下方式使用BST。

  int main(void){
      struct Emp e1  = { 1, "John" };
      struct BST *t = create_tree();
      t->set_compare( &compare );

      t->insert(e1);


      t->get(e1);
       ...
  }

  int compare(void *e1 , void *e2)
  {
      //type cast e1, e2 as struct Emp 
      // return the comparison result based on id
  } 

The advantage i see is i don't need to keep on pass this function pointer into my all BST operation functions . 我看到的优点是我不需要继续将此函数指针传递到我的所有BST操作函数中。

but storing all public functions inside struct will bring the OOP style inside C code , like what others says. 但是将所有公共函数存储在struct中会将OOP样式带入C代码中,就像其他人所说的那样。

Assume the function takes 2 variables and calls 4 different functions. 假设该函数接受2个变量并调用4个不同的函数。 Let there be a structure as follows: 让结构如下:

/* Input 1                Input 2                 Function pointer
{
{ 0,                       0,                   Function00},
{ 0,                       1,                   Function01},
{ 1,                       0,                   Function10},
{ 1,                       1,                   Function11}
}

It would be easy to compare the input values against the structure values and call corresponding function. 将输入值与结构值进行比较并调用相应的函数会很容易。

It might seem it is better to use if..else... But think of cases in which there are more than 100 such cases to be checked 使用if..else似乎更好......但想想有超过100个此类案例需要检查的情况

the benifit of having function pointer defined in the structure, is related to the code design. 在结构中定义函数指针的好处与代码设计有关。 the goal is to get your code more structured. 目标是让您的代码更有条理。

Defining variables and functions in a structure is like defining class in oriented object languages 在结构中定义变量和函数就像在面向对象语言中定义类一样

Refer to the following link for more details 有关更多详细信息,请参阅以下链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM