A summary of the C + + function pointer usage

Source: Internet
Author: User



C + + function Pointers

A function pointer describes


A function pointer points to a particular type, and the type of the function is determined by its parameters and return type, regardless of the function name. Examples are as follows:

int add (int nleft,int nright);//function definition

The function type is int (int,int), and to declare a pointer to the class function, simply substitute the pointer with the name of the functor:

Int (*PF) (int,int);//Not initialized

The pf can point to a function of type int (int,int) . PF is preceded by a *, which indicates that PF is a pointer, the right side is a formal parameter list, the pf points to a function, the left is an int, and the function returned by PF points to a value of int. The pf can point to a function of type int (int,int) . When the add type is int (int,int), the pf can point to the add function.

PF = add;//The function pointer to a specific function by assigning a value

Note: The brackets at both ends of the *PF are necessary, otherwise defined as follows:

    int *PF (int,int);//<span style= "font-family: the song Body;" > At this time <span lang= "en-US" >pf</span> is a function that returns a value of <span lang= "en-US" >int* </span>, not a function pointer. </span>



Two standard C function pointers


1 definition of function pointers

1.1

      Int (*PF) (int,int);


1.2

      typedef int (*PF) (int,int);      PF pf;//<span style= "font-family: Song body; font-size:14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " > At this point, <span lang= "en-US" >PF</span> is a function pointer type that points to a type function, rather than a specific pointer, it can be used to define a specific pointer <span lang= "en-US" >pf< /SPAN>. </span>


2 general use of function pointers

  PF = add;     PF (100,100);//The function used to point to the same     (*PF) (100,100);//Here *PF both brackets are necessary

Note: Add The type must exactly match the type of function the pf can point to.



3 function pointers as formal parameters

     The second parameter is a function type, which is automatically converted to a pointer to such a function     Void fuc (int nvalue,int pf (int,int));     An equivalent declaration that shows a parameter defined as a pointer to a function     Void fuc (int nvalue,int (*PF) (Int,int));     Void fuc (int nvalue,pf);

  fucfunction Call

  PF = ADD;//PF is a function pointer      fuc (1,ADD);//add is automatically converted to function pointer      fuc (1,PF);

4 returns a pointer to a function

4.1

   PF fuc2 (int);//PF as function pointer type

4.2
Description: read this declaration statement in an inward-to-outer order. FUC2 A tangible parameter list, then FUC2 is a function whose formal parameter is fuc2 (int), FUC2 preceded by *, so fuc2 returns a pointer, The pointer itself also contains a formal parameter list (int,int), so the pointer points to the function, and the return value of the function is Int.

Summary:FUC2 is a function that has a parameter of (int) and returns a pointer to a function that points to Int (int,int) .


Two C + + function pointers


1 because C + + is fully compatible with C, the function pointer usages available in C are available for C + +


2c++ Other functions (pointers) definition and use


2.1

  typedef decltype (ADD) add2;

decltype returns the function type , ADD2 is the same type of function as add , unlike Add2 , which is a type, not a specific function.


How to use:

<span style= "color: #000000;" >  add2* pf;//pf Pointer to the add type, uninitialized </span>


2.2

 typedef decltype (ADD) * Pf2;//<span lang= "en-US" style= "font-family: Song body; Font-size: 14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " ><span style= "Mso-spacerun:yes;" > </span>pf2</span><span style= "font-family: Song body; font-size:14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " > <span lang= "en-US" >1.1</span> <span lang= "en-us" >PF</span> same meaning </span><span Style= "font-family: Song body; font-size:14px;" ></span> 
       <span style= "font-family: Song body; font-size:14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " ><span lang= "en-US" >pf2 pf;//pf</span> pointer to <span lang= "en-us" >add</span> type function pointer, uninitialized </ Span><span style= "font-family: Song body; font-size:14px;" > </span>


2.3

  Auto PF = ADD;//PF can be considered an alias of add (personal understanding)       Auto *PF = ADD;//PF is a pointer to add

3 function Pointer parameter

typedef decltype (add) add2;   typedef decltype (ADD) * PF2;   void Fuc2 (add2 add);//function parameter, call is automatically converted to function pointer   void FUC2 (PF2 add);//function pointer parameter, passing in the corresponding function (pointer) can be

4returns a pointer to a function


4.1

      Auto Fuc2 (int), int (*) (int,int)//<span lang= "en-US" style= "font-family: Song body; font-size:14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " >fuc2</span><span style= "font-family: Song body; font-size:14pt; Mso-ascii-theme-font:major-fareast; Mso-fareast-theme-font:major-fareast; Mso-hansi-theme-font:major-fareast; " > Return function pointer to <span lang= "en-US" >int (*) (Int,int) </span></span>


4.2

Decltype (ADD) * FUC2 (int)//explicitly know which function to return, use the Decltype keyword to infer its function type,

5 member function Pointers

5.1 Normal member function pointer

    

Class a//defines classes a{private:       int Add (int nleft, int nright)       {              return (nleft + nright);       } Public:       void fuc ()       {              printf ("Hello  world\n")}                  }; typedef void (A::* PF1) ();//The name of the pointer must be preceded by A class name qualification PF1 PF1 = &A::fuc; Must have &a a;//member function address dereference must be attached to an object address, so you must create a formation (A.*PF1) ();//Use the member function pointer to call the function

5.2 function pointers in inheritance

Class A{public:       void fuc ()       {              printf ("Hello fuc () \ n");       }       void Fuc2 ()       {              printf ("Hello a::fuc2 () \ n");       }}; Class B:public A{public:       virtual void fuc2 ()       {              printf ("Hello b::fuc2 () \ n");       }}; typedef void (A::* PF1) (), typedef void (B::* PF2) (); PF1 PF1 = &a::fuc;int Main ()       {       a A;       b b;       (A.*PF1) ();  Call A::fuc       (B.*PF1) ();   Call A::fuc       PF1 = &A::fuc2;       (A.*PF1) ();  Call A::fuc2       (B.*PF1) ();  Call A::fuc2       PF2 pf2 = &A::fuc2;        (B.*PF2) (); Call A::FUC2}

6 pointers to overloaded functions

Void fuc ();   Void fuc (int);   void (*PF) (int) = FUC;//PF points to fuc (int)   int (*PF2) (int) = fuc;//error no matching type
The compiler chooses that function by its pointer type, and the pointer type must match exactly one of the overloaded functions.


A summary of the C + + function pointer usage

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.