函数指针

  • 定义函数指针模板: bool (*pf)(const string &, const string &);
    *pf两端的括号是必须的
    使用函数指针:
     1 #include <iostream>
     2 using namespace std;
     3 int func(int a, int b)
     4 {
     5     return a+b;
     6 }
     7 int main()
     8 {
     9    int (*p_func) (int a, int b) = func;
    10     cout << p_func(1,2)<<endl;
    11     return 0;
    12 }
  • 用typedef简化函数指针
    如果要多次使用该类指针,直接用p_func定义新的指针即可
     1 #include <iostream>
     2 using namespace std;
     3 typedef int (*p_func) (int a, int b);
     4 int func(int a, int b)
     5 {
     6     return a+b;
     7 }
     8 int main()
     9 {
    10     p_func p_temp = func;
    11     cout << p_temp(1,2)<<endl;
    12     return 0;
    13 }
  • 函数名前可加&,可不加,直接引用函数名等效于在函数名上应用取地址操作符,函数指针前加0表示该指针不指向任何函数
  • 指针可以加*,可不加
  • 若指针没有初始化或值为0,不能调用
  • 函数指针形参
    函数的形参可以是指向函数的指针
    例:
     1 #include <iostream>
     2 using namespace std;
     3 typedef int (*p_func) (int a, int b);
     4 int func(int a, int b)
     5 {
     6     return a+b;
     7 }
     8 int func2(int (*a) (int a, int b))
     9 {
    10     return a(1,2);
    11 }
    12 int main()
    13 {
    14     p_func ppp = func;
    15     cout << func2(ppp);
    16     return 0;
    17 }

  • 返回函数指针
    模板: int (*ff(int))(int*, int); 
    解释: ff(int) 为一个函数,形参为int;返回值为int (*)(int*, int),是一个函数指针,指向形参为(int*, int)返回值为int的函数
    若用typedef定义函数指针 typedef int (*PF)(int*, int); ,则函数的定义可改写为 PF ff(int); 
  • 函数的形参可以是函数,但返回值不能是函数,需是函数指针。
    函数类型形参所对应的实参将被自动转换为指向相应函数类型的指针
    1 // func is a function type, not a pointer to function!
    2 typedef int func(int*, int);
    3 void f1(func); // ok: f1 has a parameter of function type
    4 func f2(int); // error: f2 has a return type of function type
    5 func *f3(int); // ok: f3 returns a pointer to function type


    当p_func是函数类型:

     1 #include <iostream>
     2 using namespace std;
     3 typedef int p_func (int a, int b);
     4 int func(int a, int b)
     5 {
     6     return a+b;
     7 }
     8 p_func* func2(int (*a) (int a, int b))
     9 {
    10     p_func* p = func;
    11     return p;
    12 }
    13 int main()
    14 {
    15     p_func* p1 = func;
    16     cout << p1(1,2);
    17     return 0;
    18 }

    可成功输出3

    当p_func是函数指针类型

     1 #include <iostream>
     2 using namespace std;
     3 typedef int (*p_func) (int a, int b);
     4 int func(int a, int b)
     5 {
     6     return a+b;
     7 }
     8 p_func func2(int (*a) (int a, int b))
     9 {
    10     p_func p = func;
    11     return p;
    12 }
    13 int main()
    14 {
    15     p_func p1 = func;
    16     cout << p1(1,2);
    17     return 0;
    18 }

    可成功输出3

  • 指向重载函数的指针
    指针的类型必须与重载函数的一个版本精确匹配。如果没有精确匹配的函数,则对该指针的初始化或赋值都将导致编译错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值