首页后端开发ASP.NETC++函数与指针

C++函数与指针

时间2024-01-30 14:53:02发布访客分类ASP.NET浏览679
导读:收集整理的这篇文章主要介绍了C++函数与指针,觉得挺不错的,现在分享给大家,也给大家做个参考。最近在看C++ PRimer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解.From C++ Primer Plus: Chapt...
收集整理的这篇文章主要介绍了C++函数与指针,觉得挺不错的,现在分享给大家,也给大家做个参考。最近在看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一样只不过是使用函数指针名来调用函数coutp2(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#includeiostream>
    //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;
        coutp2(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++)        coutpa[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++)        coutpb[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);
        coutpdb":"*pdbendl;
        //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++函数与指针的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

指针

若转载请注明出处: C++函数与指针
本文地址: https://pptw.com/jishu/592665.html
JS构造函数-实例对象-原型对象之间的关系 C++ 之 Asio 库

游客 回复需填写必要信息