函数指针用于指向一个函数,函数名是函数体的入口地址
#define _CRT_SECURE_NO_WARNINGS #include<iostream>
using namespace std; int func(int a, int b) { cout << "1999年写的func" << endl; return 0; } int func2(int a, int b) { cout << "1999年写的func2" << endl; } int func3(int a, int b) { cout << "1999年写的func3" << endl; } //2018年想添加一个新的子业务
int func2018(int a, int b) { cout << "2018年写的func2018" << endl; } /* 方法一: 函数的返回值,函数的参数列表(形参的个数,类型,顺序) 定义一个函数类型 */ typedef int (FUNC)(int, int); //定义一个统一的接口将他们都调用起来
void my_function(int(*fp)(int, int),int a,int b) { cout << "1999年的实现这个架构业务" << endl; cout << "固定业务1" << endl; cout << "固定业务2" << endl; fp(a, b); cout << "固定业务3" << endl; } /* 方法二: 定义一个函数指针 */ typedef int(*FUNC_P)(int, int); int main() { #if 0
//方法一:
FUNC *fp = NULL; fp = func; fp(10, 20); //等价 (*fp)(10,20); //方法二
FUNC_P fp2 = NULL; fp2(100, 200); //方法三
int(*fp3)(int, int) = NULL; #endif my_function(func, 10, 20); my_function(func2, 100, 200); my_function(func3, 1000, 2000); my_function(func2018, 2018, 2018); return 0; }