函數指針以及在類成員函數中應用函數指針

什麼是函數指針

如果在程序中定義了一個函數,那麼在編譯時系統就會爲這個函數代碼分配一段存儲空間,這段存儲空間的首地址稱爲這個函數的地址。而且函數名錶示的就是這個地址。既然是地址我們就可以定義一個指針變量來存放,這個指針變量就叫作函數指針變量,簡稱函數指針。

那麼這個指針變量怎麼定義呢?雖然同樣是指向一個地址,但指向函數的指針變量同我們之前講的指向變量的指針變量的定義方式是不同的。例如:

int(*p)(int, int);

這個語句就定義了一個指向函數的指針變量 p。首先它是一個指針變量,所以要有一個“*”,即(*p);其次前面的 int 表示這個指針變量可以指向返回值類型爲 int 型的函數;後面括號中的兩個 int 表示這個指針變量可以指向有兩個參數且都是 int 型的函數。所以合起來這個語句的意思就是:定義了一個指針變量 p,該指針變量可以指向返回值類型爲 int 型,且有兩個整型參數的函數。p 的類型爲 int(*)(int,int)。大家需要記住這種類型,前面int表示返回值,中間括號括起來表示是一個指針,後面的括號表示參數類型列表。

所以函數指針的定義方式爲:

函數返回值類型 (* 指針變量名) (函數參數列表);

“函數返回值類型”表示該指針變量可以指向具有什麼返回值類型的函數;“函數參數列表”表示該指針變量可以指向具有什麼參數列表的函數。這個參數列表中只需要寫函數的參數類型即可。

我們看到,函數指針的定義就是將“函數聲明”中的“函數名”改成“(*指針變量名)”。但是這裏需要注意的是:“(*指針變量名)”兩端的括號不能省略,括號改變了運算符的優先級。如果省略了括號,就不是定義函數指針而是一個函數聲明瞭,即聲明瞭一個返回值類型爲指針型的函數。

那麼怎麼判斷一個指針變量是指向變量的指針變量還是指向函數的指針變量呢?首先看變量名前面有沒有“*”,如果有“*”說明是指針變量;其次看變量名的後面有沒有帶有形參類型的圓括號,如果有就是指向函數的指針變量,即函數指針,如果沒有就是指向變量的指針變量。

最後需要注意的是,指向函數的指針變量沒有 ++ 和 -- 運算。

如何用函數指針調用函數

給大家舉一個例子:

# include <stdio.h>
int Max(int, int);  //函數聲明
int main(void)
{
    int(*p)(int, int);  //定義一個函數指針
    int a, b, c;
    p = Max;  //把函數Max賦給指針變量p, 使p指向Max函數
    printf("please enter a and b:");
    scanf("%d%d", &a, &b);
    c = (*p)(a, b);  //通過函數指針調用Max函數
    printf("a = %d\nb = %d\nmax = %d\n", a, b, c);
    return 0;
}
int Max(int x, int y)  //定義Max函數
{
    int z;
    if (x > y)
    {
        z = x;
    }
    else
    {
        z = y;
    }
    return z;
}

輸出結果是:
please enter a and b:3 4
a = 3
b = 4
max = 4

我們再舉一個例子:


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <set>
#include <string>
#include <functional>
using namespace std;

int getMax(int a, int b){
	if (a >= b){
		return a;
	}
	return b;
}
int getMin(int a, int b){
	if (a <= b){
		return a;
	}
	return b;
}
int mymap(string str, int a, int b){
	map<string, int(*)(int, int)>mp;
	mp["getMin"] = &getMin;
	mp["getMax"] = &getMax;
	return (*mp[str])(a, b);
}

int main(){
	
	cout << mymap("getMax", 2, 6) << endl;
	cout << mymap("getMin", 1, 7) << endl;
	system("pause");
	return 0;
}

那麼怎麼把類成員函數作爲函數指針使用呢?

看看下面的例子:


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <set>
#include <string>
#include <functional>
using namespace std;
class pointertest{
public:
	pointertest();
	~pointertest(){};

public:
	int func1(int a, int b);
	int func2(int a, int b);
	int func3(int a, int b);
	int realfunc(string str, int a, int b);
	unordered_map<string, int(pointertest::*)(int, int)> mp;
	
};
pointertest::pointertest(){
	mp["func1"] = &pointertest::func1;
	mp["func2"] = &pointertest::func2;
	mp["func3"] = &pointertest::func3;
}
int pointertest::func1(int a, int b){
	cout << "func1" << endl;
	return a + b;
}
int pointertest::func2(int a, int b){
	cout << "func2" << endl;
	return a + b;
}
int pointertest::func3(int a, int b){
	cout << "func3" << endl;
	return a + b;
}
int pointertest::realfunc(string str, int a, int b){
	//return (this->*(mp[str]))(a, b);
	return (this->*(mp[str]))(a, b);
}

int getMax(int a, int b){
	if (a >= b){
		return a;
	}
	return b;
}
int getMin(int a, int b){
	if (a <= b){
		return a;
	}
	return b;
}
int mymap(string str, int a, int b){
	map<string, int(*)(int, int)>mp;
	mp["getMin"] = &getMin;
	mp["getMax"] = &getMax;
	return (*mp[str])(a, b);
}

int main(){
	
	pointertest a;
	int s = a.realfunc("func1", 3, 4);
	cout << s << endl;
	s = a.realfunc("func2", 3,5);
    cout<<s<<endl;
	system("pause");
	return 0;
}

/*輸出是:
func1
7
func2
8
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章