函數(shù)指針學(xué)習(xí)1.3|函數(shù)指針作為函數(shù)參數(shù)
#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)//第一個參數(shù) 是一個 指向返回值為int、函數(shù)參數(shù)為int的 函數(shù)指針,這里的指針名為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、函數(shù)參數(shù)為int的 函數(shù)指針
? ? fp f = test;
? ? cout<<test2(f, 1)<<endl; // 調(diào)用 test2 的時候,把test函數(shù)的地址作為參數(shù)傳遞給了 test2
? ? return 0;
}
輸出結(jié)果:
i am here in func_test2
i am here in func_test
10
標(biāo)簽: