《C++ Primer Plus 第六版》P246-247代碼參考P248中的7.10.4中的typedef進行簡化後的結果

所念皆山海 2021-08-16 02:01:53 阅读数:194

本文一共[544]字,预计阅读时长:1分钟~
c++ primer 第六版 第六 六版

//P246

#include<iostream>

using namespace std;

typedef const double *(*p_fun)(const double*,int);

const double *f1(const double ar[],int n);

const double *f2(const double [],int);

const double *f3(const double *,int);

int main()

{

    double av[3]={1112.3,1542.6,2227.9};

    //const double *(*p1)(const double *,int)=f1;

    p_fun p1=f1;

    auto p2=f2;

    cout<<"Using pointers to functions:"<<endl;

    cout<<"Address Value"<<endl;

    cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl;

    cout<<p2(av,3)<<": "<<*p2(av,3)<<endl;

    cout<<"----------------------------------------------"<<endl;

    cout<<"B區開始輸出"<<endl;

    //const double *(*pa[3])(const double *,int)={f1,f2,f3};

    p_fun pa[3]={f1,f2,f3};

//    auto pb=pa;

cout<<"這裏是替換處"<<endl;

    const double *(**pb)(const double *,int)=pa;

    cout<<"Using an array of pointers to functions"<<endl;

    cout<<"Address Value"<<endl;

    for(int i=0;i<3;i++)

        cout<<pa[i](av,3)<<":"<<*pa[i](av,3)<<endl;

        cout<<"----------------------------------------------"<<endl;

        cout<<"C區開始輸出"<<endl;

    cout<<"Using a pointer  to a pointer to a function:"<<endl;

    cout<<"Address Value"<<endl;

    for(int i=0;i<3;i++)

        //P247

        cout<<pb[i](av,3)<<": "<<*pb[i](av,3)<<endl;

        cout<<"pb[i]輸出完畢"<<endl;

    cout<<"----------------------------------------------"<<endl;

    cout<<"這裏開始D區的輸出"<<endl;

    cout<<"Using pointers to an array of pointers"<<endl;

    cout<<"Address value"<<endl;

    auto pc = &pa;

    cout<<"下方開始加很多*進行嘗試"<<endl;

    cout<<(**pc)[0](av,3)<<": "<<*((*pc)[0])(av,3)<<endl;

//    const  double *(*(*pd)[3])(const double *,int)=&pa;

  p_fun (*pd)[3]=&pa;   

    const double *pdb=(*pd)[1](av,3);

    cout<<"此處開始輸出pdb"<<endl;

    cout<<"pdb = "<<pdb<<endl;

    cout<<"*pdb = "<<*pdb<<endl;

    cout<<(*(*pd)[2])(av,3)<<": "<<*(*(*pd)[2])(av,3)<<endl;

    return 0;

}

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;

}

版权声明:本文为[所念皆山海]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210816020110079g.html