#include <stdio.h>
#include <stdlib.h>
/*---------------------自定义函数类型--------------------*/
typedef int sum(int a, int b);
typedef struct func_st{
sum *p_sum;
}func_t;
int int_add(int a, int b)
{
return (a + b);
}
int main()
{
func_t *my_func = NULL;
int sum;
my_func = (func_t *)malloc(sizeof(func_t));
my_func->p_sum = int_add;
sum = my_func->p_sum(10, 20);
printf("-----sum = %d\n", sum);
free(my_func);
my_func = NULL;
return 0;
}
测试结果:
g++ test.cpp -o test
./test
-----sum = 30