C + + functions and pointers

Source: Internet
Author: User
Recently in the C + + primer Plus, the sense function and pointer this chapter more difficult, write notes, strengthen understanding.

From C + + Primer Plus:chapter 7 function:c++ Programming Modules

1. How do I declare a function pointer?

Similar to function prototypes: You need to declare the pointer to the function's return value and argument list


Double pam (int); The argument is of type int, a function that returns a double type of double (*PF);(int)  //points to an int type, and a pointer pf = Pam with a double type returned value;   The function name represents the address of a double x = Pam (4); The function name is called double x = (*PF) (4); The pointer is called Double x = PF (4); C + + also allows pointer names to be used as function names

2. C + + 11 automatic type inference


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. Use the pointer name as a function name


The preceding function is a double * type, cout the first part returns a double pointer, the second part returns the value pointed to by the double pointer cout<< (*P1) (av,3) << ":" <<* (*P1) (av,3) <<endl;//, like the cout above, simply calls the function using the function pointer name COUT<<P2 (av,3) << ":" <<*p2 (av,3) <<endl;

4. Array of function pointers


Const double * (*PA[3]) (const double *,int) = {F1,F2,F3}; Create an array of function pointers//call functions via pointers, get the returned pointer const double *PX = pa[0] (av,3); Call by pointer as if it were a function nameconst double *py = (*pa[0]) (av,3); The normal call//Get function returns the value that the pointer points to double x = *pa[0] (av,3);d ouble x = * (*pa[0]) (av,3);

5. Pointers to pointer arrays

The difference between pointer arrays and arrays of pointers


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

Pointer to array



1 Auto PC = &pa; &PA is the address of the entire array, and the PA is the first element of the array

2

3 Const DOUBLE * (* (*PD) [3]) (const double *, int) = &pa; And the first equivalent of

4

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

Code:


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++ 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&LT;&LT;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 is a pointer to an array of function pointers cout<< "\nusing pointers to an array of pointers:\n";    cout<< "Address value\n";    Easy-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-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;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.