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
DoublePamint);//the parameter is of type int and the return value is a function of type doubleDouble(*pf);(int)//a pointer to the type int with the return value of type doublePF = pam;//the function name represents the address of the functionalDoublex = Pam (4);//Function name CallDoublex = (*pf) (4);//Pointer callsDoublex = PF (4);//C + + also allows pointer names to be used as function names
2. C + + 11 automatic type inference
Const Double * F1 (constdoubleint); Const Double * (*P1) (constdoubleint//p1 poitns to 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; // just like the cout above, the function is called 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};//creating an array of function pointers//call a function with a pointer to get the returned pointerConst Double*PX = pa[0] (av,3);//Call by pointer as if it were a function nameConst Double*py = (*pa[0]) (av,3);//Normal Call//gets the value that the function returns pointer toDoublex = *pa[0] (av,3);Doublex = * (*pa[0]) (av,3);
5. Pointers to pointer arrays
The difference between pointer arrays and arrays of pointers
*pd[3//anarray of 3 pointers(*pd) [3//A pointer to an array of three ele ments
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]) (constdouble *, int// And the first equivalent of 45 **&pa = *PA = pa[0]
Code:
1 //Arfupt.cpp--an array of function pointers2#include <iostream>3 //various notations,same signatures4 Const Double*F1 (Const Doublear[],intn);5 Const Double*F2 (Const Double[],int);6 Const Double*F3 (Const Double*,int);7 8 intMain ()9 {Ten using namespacestd; one Doubleav[3] = {1112.3,1542.6,2227.9}; a - //pointer to a function - the 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; aCOUT<<P2 (av,3) <<":"<<*P2 (av,3) <<endl; at - //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; in //pre-c++11 can use the following code instead - //Const DOUBLE * (**pb) (const double *, int) = pa; tocout<<"\nusing An array of pointers to functions:\n"; +cout<<"Address value\n"; - for(inti =0; I <3; i++) thecout<<pa[i] (av,3) <<":"<<*pa[i] (av,3) <<endl; *cout<<"\nusing A pointer to a pointer to a function:\n"; $cout<<"Address value\n";Panax Notoginseng for(inti =0; I <3; i++) -cout<<pb[i] (av,3) <<":"<<*pb[i] (av,3) <<endl; the + //what's A pointer to an array of function pointers acout<<"\nusing Pointers to an array of pointers:\n"; thecout<<"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 the Const Double* (* (*pd) [3])(Const Double*,int) = &pa; - //store return value in PDBWuyi Const Double*pdb = (*pd) [1] (av,3); thecout<<pdb<<":"<<*pdb<<endl; - //Alternative notation wucout<< (* (pd) [2]) (av,3) <<":"<<* (* (*pd) [2]) (av,3) <<endl; - } about $ Const Double* F1 (Const Double* ar,intN) - { - returnar; - } a Const Double* F2 (Const Doublear[],intN) + { the returnCri1; - } $ Const Double* F3 (Const Doublear[],intN) the { the returnar+2; the}
"c + +" functions and pointers