溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++程序的函數(shù)指針實(shí)際操作介紹

發(fā)布時(shí)間:2021-09-10 07:38:36 來(lái)源:億速云 閱讀:126 作者:chen 欄目:編程語(yǔ)言

這篇文章主要介紹“C++程序的函數(shù)指針實(shí)際操作介紹”,在日常操作中,相信很多人在C++程序的函數(shù)指針實(shí)際操作介紹問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++程序的函數(shù)指針實(shí)際操作介紹”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

先看看這段代碼:

#includeusing std::cout;using std::endl; inline int min(int a,int b){return (a>b) ? b : a;} int Min(int a,int b,int (*pf)(int,int))//可以使用缺省參數(shù):int Min(int a,int b,int (*pf)(int,int)=min){return pf(a,b); //通過(guò)函數(shù)指針來(lái)調(diào)用函數(shù),也可以寫(xiě)為 //return (*pf)(a,b);作用是一樣的。} int main(int argc, char* argv[]){int i=1;int j=10; int r=Min(i,j,min); //如果使用缺省參數(shù)的話,可以寫(xiě)成:int r=Min(i,j);cout<<1r<1<1endl;
return 0;
}

其中int (*pf)(int,int)定義了一個(gè)返回值為int,參數(shù)為兩個(gè)int的函數(shù)指針。如果不在*pf上加括號(hào)的話,即:

int *pf(int,int),

編譯器會(huì)把它解釋為一個(gè)返回值為整型指針,參數(shù)為兩個(gè)int的函數(shù)。

可以用typedef來(lái)簡(jiǎn)化代碼:

#includeusing std::cout;using std::endl;typedef int (*PF)(int,int);//這行代碼是關(guān)鍵,相當(dāng)與把上個(gè)例子中的函數(shù)指針聲明為一種數(shù)據(jù)類型。 inline int min(int a,int b){return (a>b) ? b : a;}int Min(int a,int b,PF f) //PF f定義f為和上個(gè)例子中一樣的函數(shù)指針。{return f(a,b);}int main(int argc, char* argv[]){int i=1;int j=10;int r=Min(i,j,min);cout<<1r<1<1endl;
return 0;
}

也可以提供一個(gè)用模板實(shí)現(xiàn)的函數(shù)指針:

#includeusing std::cout;using std::endl;inline int min(int a,int b){return (a>b) ? b : a;}templateT Min(T a,T b,T (*pf)(T,T)){return pf(a,b);}int main(int argc, char* argv[]){int i=1;int j=10;int r=Min(i,j,min); //int r=Min(i,j,min); 這種形式編譯器會(huì)報(bào)錯(cuò):Expression syntaxcout<<1r<1<1endl;
return 0;
}

當(dāng)然,這個(gè)指針指向的函數(shù)也可用模板來(lái)實(shí)現(xiàn):

#includeusing std::cout;using std::endl; templateinline T min(T a,T b){return (a>b) ? b : a;}templateT Min(T a,T b,T (*pf)(T,T)){return pf(a,b);}int main(int argc, char* argv[]){long i=2000000;long j=1000000;//使用時(shí)有三種形式:long r=Min(i,j,min);//第一種。注意這里在min后一定要加,否則編譯器將報(bào)錯(cuò)://Could not find a match for "Min(long,long,T(*)(T,T)"//第二種:long r=Min(i,j,min);//第三種:long r=Min(i,j,min);//其實(shí)質(zhì)是一樣的。cout<<1r<1<1endl;
return 0;
}

不過(guò)我不能用typedef使代碼更為簡(jiǎn)便,就像下面這種形式:

template

typedef T (*PF)(T,T);

編譯器會(huì)提示:Templates must be classes or functions

另外還可以使用函數(shù)指針的數(shù)組:

#includeusing std::cout;using std::endl;inline int min(int a,int b){return (a>b) ? b : a;}inline int max(int a,int b){return (a>b) ? a : b;}int main(int argc, char* argv[]){int i=1;int j=10; int (*pf[2])(int,int);//擁有兩個(gè)元素的函數(shù)指針數(shù)組,每個(gè)元素是返回值為int,參數(shù)為兩個(gè)int的函數(shù)指針。pf[0]=min;pf[1]=max; int r1=pf[0](i,j);int r2=pf[1](i,j);cout<<1r1<1<1endl;cout<1<1r2<1<1endl;
return 0;
}

指向重載函數(shù)的指針也是值得注意的:#include using std::cout;using std::endl;

inline void print(int a){ cout<<1a<1<1endl;}inline void="" print(long="" b){="" cout<1<1b<1<1endl;}
int main(int argc, char* argv[]){ int i=1; long m=100000;
 void (*pf1)(int)=print; void (*pf2)(long)=print;
 pf1(i); pf2(m);

return 0;}程序運(yùn)行的很成功。因?yàn)榫幾g器會(huì)自動(dòng)查找所有的重載函數(shù),以找到和函數(shù)指針指向的函數(shù)具有相同的返回類型和參數(shù)表的函數(shù)。

如上我們可知聲明一個(gè)給定函數(shù)的函數(shù)指針的一般規(guī)則:即這個(gè)函數(shù)指針的返回類型和參數(shù)表必須和給定的函數(shù)相同。要注意省略號(hào)也是函數(shù)類型的一部分,int function1(int,...)與int function2(int)需要兩個(gè)不同的函數(shù)指針。其實(shí)函數(shù)名就是指向該函數(shù)的指針,對(duì)于int function(int)來(lái)說(shuō),function就是它的指針。我們可用這個(gè)特性對(duì)函數(shù)指針進(jìn)行初始化:int (*pf)(int)=function;

取地址操作符也可以用在函數(shù)名上,上面的代碼和int (*pf)(int)=&function的作用是一樣的。

到此,關(guān)于“C++程序的函數(shù)指針實(shí)際操作介紹”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI