概要:
- 函數指針介紹
- typedef簡化函數指針的定義
- 指向函數的指針的初始化和賦值
- 通過指針調用函數
- 函數指針形參
- 返回指向函數的指針
- 指向重載函數的指針
參考《C++ Primer》 第五版
- 函數指針介紹
函數指針是C++中比較靈活而且重要的部分,對於軟件的靈活度上有很大的幫助 !
函數指針指向的是函數而非對象,和其他指針一樣,函數指針指向某種特定類型,函數的類型由它的返回類型和形參類型共同決定,與函數名無關。
bool lengthCompare(const string &,const string &);
該函數的類型是bool(const string& ,const string&)。想要聲明一個指向改函數的指針,只需要用指針特換函數名即可:
bool (*pf)(const string&, const string&);//未初始化
Note: *pf 兩端的括號必不可少,如果不寫括號,則pf是一個返回值為bool的指針的函數:
//聲明一個名為pf的函數,該函數返回bool*
bool *pf(const string &, const string &);
- typedef簡化函數指針的定義
現在我們來定義三個函數指針:
bool(*pf1)(const string &, const string &); bool(*pf2)(const string &, const string &); bool(*pf3)(const string &, const string &);
有沒有發現一個問題,每次定義都需要這么長,有沒有好的辦法呢,當然是有的,我們可以用到typedef:
typedef bool(*cmpFcn)(const string &, const string &); //bool(*pf1)(const string &, const string &); //bool(*pf2)(const string &, const string &); //bool(*pf3)(const string &, const string &);
cmpFcn pf1; cmpFcn pf2; cmpFcn pf3;
- 指向函數的指針的初始化和賦值
函數指針的賦值:
pf = lengthCompare ; pf = &lengthCompare ;
因為在C/C++里函數名就是地址,所以以上兩者都等價,都可以給函數指針賦值。
- 通過指針調用函數
可以直接使用指向函數的指針調用函數,無須提前解引用:
bool b1=pf("hello","goodbye"); bool b2=(*pf)("hello","goodbye"); bool b3=LengthCompare("hello","goodbye"); //三個等價調用
指向不同函數類型的指針之間不存在轉換規則。
我們可以為函數指針賦一個 nullptr或者0 的整型常量表達式,表示該指針沒有指向任何一個函數。
bool lengthCompare(const string &s1, const string &s2) { return s1.size() == s2.size(); } string::size_type sumLength(const string &s1, const string &s2) { return s1.size() + s2.size(); } bool cstringCompare(char *s1, char *s2) { return strlen(s1) == strlen(s2); } pf1 = 0; pf2 = lengthCompare; //pf3 = sumLength; //pf4 = cstringCompare;
如果函數返回類型不匹配(如上)、或者形參不匹配,也會報錯!
- 函數指針形參
雖然不能定義函數類型的形參,但是形參可以是指向函數的指針。此時,形參看起來是函數類型,實際上確實當成指針使用:
#include<iostream> #include<string>
using namespace std; typedef bool(*cmpFcn)(const string &, const string &); //bool(*pf1)(const string &, const string &); //bool(*pf2)(const string &, const string &); //bool(*pf3)(const string &, const string &);
bool lengthCompare(const string &s1, const string &s2) { return s1.size() == s2.size(); }//第三個形參是函數類型,它會自動地轉成指向函數類型的指針
void useBigger(const string &s1, const string &s2, bool(*pf)(const string&, const string&)) { cout << pf(s1, s2) << endl; } int main() { cmpFcn pf = lengthCompare; useBigger("hi", "func", pf); system("pause"); return 0; }
- 返回指向函數的指針
和數組類似,雖然不能返回一個函數,但是能返回指向函數類型的指針,然而我們必須把返回類型寫成指針形式,編譯器不會自動地將函數返回類型當成對應的指針類型處理。
#include<iostream> #include<string> #include<vector>
using namespace std; int demo(int *p, int a) { return 12; } // ff是一個函數,有一個形參x,返回結果是一個函數指針int(*)(int *,int)
int (*ff(int x))(int *, int) { cout << x << endl; return demo; } int main() { int a = 5; cout << ff(2)(&a, a)<< endl; system("pause"); return 0; }
同樣,要想聲明一個返回函數類型的指針,最簡單的方法是使用類型別名:
typedef int (*PF)(int *, int);
PF是一個函數指針,指向的函數有兩個形參。
於是就可以這么寫了:
// ff是一個函數,有一個形參x,返回結果是一個函數指針int(*)(int *,int) //int(*ff(int x))(int *, int) { // cout << x << endl; // return demo; //}
PF ff(int x) { cout << x << endl; return demo; }
- 指向重載函數的指針
編譯器通過指針類型決定選取那個函數,指針類型必須與重載函數中的一個精確匹配。
#include<iostream> #include<string> #include<vector>
using namespace std; void ff(vector<double> vec) { cout << "ff(vector<double vec)" << endl; } void ff(unsigned int x) { cout << "ff(unsigned int x)" << endl; } int main() { //void(*pf)(int x) = &ff; 報錯
void (*pf)(unsigned int x) = &ff; //double (*pf2)(vector<double>) = &ff; 報錯
void (*pf2)(vector<double>) = &ff; system("pause"); return 0; }