C++函數與指標

來源:互聯網
上載者:User
最近在看C++ primer plus,感覺函數與指標這一章痛點比較多,記寫筆記,加強理解.

From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules

1. 如何聲明函數指標?

和函數原型類似: 需要聲明指標指向函數的傳回值和參數列表


double pam(int); //參數為int 類型,傳回值為double 類型的函數double (*pf);(int)  //指向參數為int類型,傳回值為double 類型的指標pf = pam;   //函數名代表了函數的地址double x = pam(4); //函數名調用double x = (*pf)(4); //指標調用double x = pf(4); //C++也允許將指標名當作函數名使用

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. 將指標名當作函數名使用


//前面函數為double *類型,cout第一部分返回double指標,第二部分返回double指標指向的值cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl;//和上面的cout一樣只不過是使用函數指標名來調用函數cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;

4. 函數指標數組


const double *(*pa[3]) (const double *,int) = {f1,f2,f3}; //建立函數指標數組//通過指標調用函數,得到返回的指標const double *px = pa[0](av,3); //call by pointer as if it were a function nameconst double *py = (*pa[0])(av,3); //正常調用//得到函數返回指標指向的值double x = *pa[0](av,3);double x = *(*pa[0])(av,3);

5. 指向指標數組的指標

指標數組和數組指標的區別


*pd[3] //an array of 3 pointers(*pd)[3] //a pointer to an array of three elements

指向數組的指標



1 auto pc = &pa; //&pa是整個數組的地址, pa是數組第一個元素首地址

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 signaturesconst 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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.