c语言实现

1 #include<stdio.h>
2 struct qwer
3 {
4 int a;
5 void (*b)(struct qwer); //定义函数指针变量
6 };
7 void myprintf(struct qwer a)
8 {
9 printf("myprintf is %d\n",a.a);
10 }
11 int main()
12 {
13 struct qwer a={1,myprintf}; //赋值直接 赋函数名字!!!!!!!
14 printf("is %d\n",a.a);
15 a.b(a); //通过变量调用函数!!!!!!!!
16
17 }

c++实现

1 #include<stdio.h>
2 class qwer
3 {
4 public:
5 int a;
6
7
8 void myprintf()
9 {
10 printf("myprintf is %d\n",a);
11 }
12 };
13 int main()
14 {
15 qwer a={1};
16 printf("is %d\n",a.a);
17 a.myprintf();
18
19 }