這篇文章給大家分享的是有關C++函數(shù)與指針是什么的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules
1. 如何聲明函數(shù)指針?
和函數(shù)原型類似: 需要聲明指針指向函數(shù)的返回值和參數(shù)列表
double pam(int); //參數(shù)為int 類型,返回值為double 類型的函數(shù) double (*pf);(int) //指向參數(shù)為int類型,返回值為double 類型的指針 pf = pam; //函數(shù)名代表了函數(shù)的地址 double x = pam(4); //函數(shù)名調(diào)用 double x = (*pf)(4); //指針調(diào)用 double x = pf(4); //C++也允許將指針名當作函數(shù)名使用
2. C++ 11 自動類型推斷
const double * f1(const double *, int); const double * (*p1)(const double *, int); //p1 poitns to f1 auto p2 = f1; //C++11 automatic type deduction,p2 points to f1 as well
3. 將指針名當作函數(shù)名使用
//前面函數(shù)為double *類型,cout第一部分返回double指針,第二部分返回double指針指向的值 cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; //和上面的cout一樣只不過是使用函數(shù)指針名來調(diào)用函數(shù) cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;
4. 函數(shù)指針數(shù)組
const double *(*pa[3]) (const double *,int) = {f1,f2,f3}; //創(chuàng)建函數(shù)指針數(shù)組 //通過指針調(diào)用函數(shù),得到返回的指針 const double *px = pa[0](av,3); //call by pointer as if it were a function name const double *py = (*pa[0])(av,3); //正常調(diào)用 //得到函數(shù)返回指針指向的值 double x = *pa[0](av,3); double x = *(*pa[0])(av,3);
5. 指向指針數(shù)組的指針
指針數(shù)組和數(shù)組指針的區(qū)別
*pd[3] //an array of 3 pointers (*pd)[3] //a pointer to an array of three elements
指向數(shù)組的指針
1 auto pc = &pa; //&pa是整個數(shù)組的地址, pa是數(shù)組第一個元素首地址
2
3 const double * (*(*pd)[3])(const double *, int ) = &pa; //和第一個等價
4
5 **&pa = *pa = pa[0]
代碼:
//arfupt.cpp -- an array of function pointers #include<iostream> //various notations,same signatures const double *f1(const double ar[],int n); const double *f2(const double [],int); const double *f3(const double *,int); int main() { using namespace std; double av[3] = {1112.3,1542.6,2227.9}; //pointer to a function const double *(*p1)(const double *,int) = f1; auto p2 = f2;//C++ 11 utomatic type deduction //pre-C++11 can use the following code instead //const double *(*p2)(const double *,int) = f2; cout<<"Using pointers to functions:\n"; cout<<"Address Value\n"; cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; cout<<p2(av,3)<<":"<<*p2(av,3)<<endl; //pa an array of pointers //auto doesn't work with list initialization const double *(*pa[3])(const double *,int) = {f1,f2,f3}; //pb a pointer to first element of pa auto pb = pa; // pre-C++11 can use the following code instead // const double *(**pb)(const double *, int) = pa; cout<<"\nUsing an array of pointers to functions:\n"; cout<<"Address Value\n"; for(int i = 0;i < 3; i++) cout<<pa[i](av,3)<<":"<<*pa[i](av,3)<<endl; cout<<"\nUsing a pointer to a pointer to a function:\n"; cout<<"Address Value\n"; for(int i = 0;i < 3; i++) cout<<pb[i](av,3)<<":"<<*pb[i](av,3)<<endl; //what about a pointer to an array of function pointers cout<<"\nUsing pointers to an array of pointers:\n"; cout<<"Address Value\n"; //easy way to declare pc auto pc = &pa; // pre-C++11 can use the following code instead // const double *(*(*pc)[3])(const double *, int) = &pa; cout<<(*pc)[0](av,3)<<":"<<*(*pc)[0](av,3)<<endl; //hard way to declare pd const double *(*(*pd)[3])(const double *,int) = &pa; //store return value in pdb const double *pdb = (*pd)[1](av,3); cout<<pdb<<":"<<*pdb<<endl; //alternative notation cout<<(*(pd)[2])(av,3)<<":"<<*(*(*pd)[2])(av,3)<<endl; } const double * f1(const double * ar, int n) { return ar; } const double * f2(const double ar[], int n) { return ar+1; } const double * f3(const double ar[], int n) { return ar+2; }
感謝各位的閱讀!關于“C++函數(shù)與指針是什么”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。