函数指针学习1.3|函数指针作为函数参数
#include <iostream>
#include <vector>
using namespace std;
int test(int a)
{
cout << "i am here in func_test\n";
return a-1;
}
int test2(int (*fun)(int),int b)//第一个参数 是一个 指向返回值为int、函数参数为int的 函数指针,这里的指针名为fun。
{
cout << "i am here in func_test2\n";
int c = fun(10)+b;
return c;
}
int main(int argc, const char * argv[])
{
typedef int (*fp)(int a);//定义一个指向 返回值为int、函数参数为int的 函数指针
fp f = test;
cout<<test2(f, 1)<<endl; // 调用 test2 的时候,把test函数的地址作为参数传递给了 test2
return 0;
}
输出结果:
i am here in func_test2
i am here in func_test
10