欢迎光临散文网 会员登陆 & 注册

函数指针学习1.3|函数指针作为函数参数

2022-01-03 22:34 作者:LogByMax  | 我要投稿


#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


函数指针学习1.3|函数指针作为函数参数的评论 (共 条)

分享到微博请遵守国家法律