在孙鑫老师第二节课视频上听到的,索性就记在这里了,省的下次又不知道怎么说了。
在C语言中,struct结构体中是不能定义成员函数的,在C++中可以。

但在C语言中可以定义函数指针变量来达到自己所要的目的,不顾这貌似也不太必要。例子如下:
struct   test
{
int   i;
void   (*pfun)(struct   test*,   int);
}

void   set(struct   test*   t,   int   n)
{
t-> i   =   n;
}

void   f()
{
test   t;
t-> pfun   =   set;
t-> pfun(&t,   2);
}

相关文章:

  • 2022-12-23
  • 2021-09-04
  • 2021-10-25
  • 2022-12-23
  • 2021-12-04
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-08-02
相关资源
相似解决方案