zoukankan      html  css  js  c++  java
  • 函数指针

    1.格式

     /* C */
     void(*fp)(void)=NULL;  // 对应的是 void fun() 类型的函数
     int(*fp)(int)=NULL;    // 对应的是 int fun(int) 类型的函数
    /* C++ */
     int(*fp)(int)=NULL;            // 对于静态成员函数(和C一样)
     int(ClassName::*fp)(int)=NULL; //对于普通成员函数要加上类域  

    2.示例

    typedef int(*fp)(int);  /** C **/
    
    int f(int a)
    {
        cout << a << endl;
        return 1;
    }
    int main()
    {
    
       fp pf = NULL;
       pf = (fp)*(&f);
       cout << pf(2) << endl;
       return 0;
          
    /* 或者不使用typedef
       int(*fp)(int)=NULL;
      pf=f;
       cout<<pf(2)<<endl;
    */
    }


    /*******************************************************************/
    class A /** C++ **/ { public: void fun1(int a){ cout << a << endl; } static void fun2(int b){ cout << b << endl; } }; int main() { A b; void(A::*fp)(int) = NULL; /*对普通成员函数的调用*/ fp = &A::fun1; (b.*fp)(2); // 调用fun1 //-----------------------------+ void(*pf)(int) = NULL; /*对静态成员函数的调用,同C一样*/ pf = b.fun2; pf(4); //调用fun2 return 0; }

      

    ------------ 转载请注明出处 ------------
  • 相关阅读:
    2021.1.11
    2021.1.10(每周总结)
    2021.1.9
    2021.1.8
    2021.1.7
    构建之法阅读笔记01
    [java] XML DTD XSD
    详解 泛型 与 自动拆装箱
    详解 正则表达式
    详解 LinkedHashMap
  • 原文地址:https://www.cnblogs.com/whlook/p/6510516.html
Copyright © 2011-2022 走看看