c/c++ 函數指針的用法

c語言函數指針的定義形式:返回類型 (*函數指針名稱)(參數類型,參數類型,參數類型,…);

c++函數指針的定義形式返回類型 (類名稱::*函數成員名稱)(參數類型,參數類型,參數類型,….);    

以下代碼編譯環境:codeblocks with gcc in win 7

c語言函數指針使用舉例: 

複製代碼
#include <stdio.h>
#include <stdlib.h>

int fun1()
{
    printf("this is fun1 call\n");
    return 1;
}

void fun2(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

int main()
{
    int (*pfun1)() = NULL;
    void (*pfun2)(int, char) = NULL;
    int a,b;
    pfun1 = fun1; //第一種賦值方法
    a = pfun1();  //第一種調用方法(推薦)
    printf("%d\n",a);
    b = (*pfun1)();//第二種調用方法
    printf("%d\n",b);
    pfun2 = &fun2;//第二種賦值方法(推薦,因爲和其他數據指針賦值方法一致)
    pfun2(1,'a');
    (*pfun2)(2,'b');
    return 0;
}
複製代碼

 

c++函數指針使用舉例:

複製代碼
#include <iostream>
using namespace std;

class test
{
public:
    test()
    {
        cout<<"constructor"<<endl;
    }
    int fun1(int a, char c)
    {
        cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
        return a;
    }
    void fun2(double d)const
    {
        cout<<"this is fun2 call:"<<d<<endl;
    }
    static double fun3(char buf[])
    {
        cout<<"this is fun3 call:"<<buf<<endl;
        return 3.14;
    }
};

int main()
{
    // 類的靜態成員函數指針和c的指針的用法相同
    double (*pstatic)(char buf[]) = NULL;//不需要加類名
    pstatic = test::fun3; //可以不加取地址符號
    pstatic("myclaa");
    pstatic = &test::fun3;
    (*pstatic)("xyz");

    //普通成員函數
    int (test::*pfun)(int, char) = NULL; //一定要加類名
    pfun = &test::fun1; //一定要加取地址符號
    test mytest;
    (mytest.*pfun)(1, 'a'); //調用是一定要加類的對象名和*符號

    //const 函數(基本普通成員函數相同)
    void (test::*pconst)(double)const = NULL; //一定要加const
    pconst = &test::fun2;
    test mytest2;
    (mytest2.*pconst)(3.33);

//    //構造函數或者析構函數的指針,貌似不可以,不知道c++標準有沒有規定不能有指向這兩者的函數指針
//    (test::*pcon)() = NULL;
//    pcon = &test.test;
//    test mytest3;
//    (mytest3.*pcon)();

    return 0;
}
複製代碼

 函數指針作爲函數參數:

複製代碼
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

void fun1(void (*pfun)(int, char), int a, char c)
{
    pfun(a, c);
}

int main()
{
    fun1(fun, 1, 'a');
    return 0;
}
// c++ 的形式差不多
複製代碼

函數指針作爲函數返回值:

複製代碼
// c 形式
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

//fun1 函數的參數爲double,返回值爲函數指針void(*)(int, char)
void (*fun1(double d))(int, char)
{
    printf("%f\n",d);
    return fun;
}

int main()
{
    void (*p)(int, char) = fun1(3.33);
    p(1, 'a');
    return 0;
}
複製代碼
複製代碼
//c++ 形式
#include <iostream>
using namespace std;

class test
{
public:
    int fun(int a, char c)
    {
        cout<<"this is fun call:"<<a<<" "<<c<<endl;
        return a;
    }
};

class test2
{
    public:
    // test2 的成員函數fun1,參數是double,
    //返回值是test的成員函數指針int(test::*)(int, char)
    int (test::*fun1(double d))(int, char)
    {
        cout<<d<<endl;
        return &test::fun;
    }
};

int main()
{
    test mytest;
    test2 mytest2;
    int (test::*p)(int, char) = mytest2.fun1(3.33);
    (mytest.*p)(1, 'a');
    return 0;
}
複製代碼

函數指針數組:

複製代碼
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}

int main()
{
    //定義一個函數指針數組,大小爲2
    //裏面存放float (*)(float, float)類型的指針
    float (*pfunArry[2])(float, float) = {&add, &minu};
    double k = pfunArry[0](3.33,2.22);// 調用
    printf("%f\n", k);
    k = pfunArry[1](3.33,2.22);
    printf("%f\n", k);
    return 0;
}
//c++ 可類比
複製代碼

typedef 簡化函數指針類型:

複製代碼
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b)
{
    printf("%f\n",a+b);
    return a+b;
}
float minu(float a,float b)
{
    printf("%f\n",a-b);
    return a-b;
}

//用pfunType 來表示float(*)(float, float)
typedef float(*pfunType)(float, float);

int main()
{
    pfunType p = &add;//定義函數指針變量
    p(3.33, 2.22);
    pfunType parry[2] = {&add, &minu};//定義函數指針數組
    parry[1](3.33, 2.22);
    //函數指針作爲參數可以定義爲:void fun(pfunType p)
    //函數指針作爲返回值可以定義爲:pfunType fun();

    return 0;
}
//c++ 可類比
複製代碼

 【版權聲明】轉載請註明出處  http://www.cnblogs.com/TenosDoIt/p/3164081.html

 

發佈了398 篇原創文章 · 獲贊 71 · 訪問量 63萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章