• 函数指针


    • 定义函数指针模板: 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

    • 指向重载函数的指针
      指针的类型必须与重载函数的一个版本精确匹配。如果没有精确匹配的函数,则对该指针的初始化或赋值都将导致编译错误
  • 相关阅读:
    redis学习笔记(三)——redis的命令大全总结
    redis学习笔记(二)——java中jedis的简单使用
    redis学习笔记(一)——windows下redis的安装与配置
    快速排序
    SpringMvc实现批量删除,使用post传值一直报404错误
    Bootstrap-table 显示行号
    选择排序
    冒泡排序
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class
    Jquery Validate动态添加和删除校验规则
  • 原文地址:https://www.cnblogs.com/2020R/p/13189070.html
Copyright © 2020-2023  润新知