C++ functions and pointers

炎欲天舞
Release: 2017-08-03 16:56:15
Original
1820 people have browsed it

I am reading C++ Primer Plus recently, and I feel that the chapter of functions and pointers is more difficult. Take notes to enhance your understanding.

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

1. How to declare function pointer?

Similar to the function prototype: you need to declare the pointer pointing to the return value and parameter list of the function


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++也允许将指针名当作函数名使用
Copy after login

2. C++ 11 automatic typing Inference

<br/>

 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
Copy after login

3. Use pointer name as function name

<br/>

//前面函数为double *类型,cout第一部分返回double指针,第二部分返回double指针指向的值
cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl;
//和上面的cout一样只不过是使用函数指针名来调用函数
cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;
Copy after login

4 . Function pointer array

<br/>

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 name
const double *py = (*pa[0])(av,3); //正常调用

//得到函数返回指针指向的值
double x = *pa[0](av,3);
double x = *(*pa[0])(av,3);
Copy after login

5. Pointer to pointer array

The difference between pointer array and array pointer

<br/>

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

Pointer to array

<br/>

<br/>
Copy after login

1 auto pc = &pa; //&pa is the entire array The address, pa is the first address of the first element of the array

2

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

4

5 **&pa = *pa = pa[0]

Code:

<br/>

//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&#39;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;
}
Copy after login

The above is the detailed content of C++ functions and pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!