#include<iostream>
using namespace std;
typedef int (*fp)(int a);//定义函数指针,之后可以用fp f;来定义一个指向函数的指针f
int test(int a)
{
return a - 1;
}
int test2(fp f, int b)//等效于int test2(int (*fun)(int ), int b)但test2(int (*fun)(int ), int b)省略了第一个形参
{
int c = f(10) + b;
return c;
}
void main()
{
cout << test2(&test, 1) << endl;//注意传入的第一个实参:“&test”
}
//下面代码等效于上面代码
//int test(int a)
//{
// return a - 1;
//}
//int test2(int (*fun)(int ), int b)
//{
// int c = fun(10) + b;
// return c;
//}
//void main()
//{
// typedef int (*fp)(int a);
// fp f = test;
// /*cout << "test2(f,1):" << test2(f, 1) << endl;*/
// cout << "test2(f,1):" << test2(&test, 1) << endl;
//
//}
C++ 函数指针 & 类成员函数指针 | 菜鸟教程 (runoob.com)