Summary of the Use of C/C++ Function Pointer

Introduction of a Function Pointer

Function pointers point to a specific type, and the type of 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). To declare a pointer to this kind of function, you only need to replace the function name with the pointer:

int (*pf)(int,int);//uninitialized  

Then PF can point to functions of type int(int,int). In front of pf, there is *, indicating that PF is a pointer, and on the right is a list of parameters, indicating that PF points to a function, and on the left is an int, indicating that the return value of the function pointed to by PF is an int. Then PF can point to functions of type int(int,int). If the add type is int(int,int), then PF can point to the add function.

pf = add;//Pointing a function pointer to a specific function by assignment  

Note: * parentheses at both ends of pf are essential, otherwise they are defined as follows:

int *pf(int,int);//In this case, pf is a function that returns an int * instead of a function pointer.  

Two Standard C Function Pointer

Definition of function pointer

1.1 Common Function Pointer Definition

int (*pf)(int,int);  

1.2 Use typedef to define function pointer types

typedef int (*PF)(int,int);  
PF pf;//In this case, to point to a function pointer type of a certain type of function, rather than a specific pointer, it can be used to define specific pointers.</span>  

Common use of function pointer

pf = add;  
pf(100,100);//It's the same as the function it points to.  
(*pf)(100,100);//Here * pf parentheses are essential  

Note: The add type must match exactly the type of function that pf can point to

3 Function Pointer as Formal Parameter

// The second parameter is a function type, which automatically converts to a pointer to such a function.

Void fuc(int nValue,int pf(int,int));  

// Equivalent declaration, showing that the formal parameter is defined as a pointer to a function

Void fuc(int nValue,int (*pf)(int,int));  
Void fuc(int nValue,PF);  

Function calls with function pointers in formal parameters. Take fuc as an example:

pf = add;//pf is a function pointer  
fuc(1,add);//add automatically converts to function pointer  
fuc(1,pf);  

4 Returns the pointer to the function

4.1 Use the function pointer type defined by typedef as the return parameter

PF fuc2(int);//PF is a function pointer type  

4.2 Define function pointer directly as return parameter

int (*fuc2(int))(int,int);//Display definition  

Note: Read the statement in an inward-outward order. If fuc2 has a list of tangible parameters, then fuc2 is a function whose formal parameters are fuc2 (int), and fuc2 has *, so fuc2 returns a pointer, which itself contains a list of tangible parameters (int, int), so the pointer points to a function whose return value is int.

Summary: fuc2 is a function with the formal parameter (int) and returns a function pointer to int(int,int).

Three C++ Function Pointer

1 Since C++ is fully compatible with C+, the usage of function pointers available in C can be used in C++.

2 C++ Definition and Use of Other Functions (Pointer)

2.1 Typeedef and decltype combine to define function type

typedef decltype(add) add2;  

decltype returns the type of function, add2 is the same type of function as add, but add2 is the type, not the specific function.

Usage method:

add2* pf;//pf points to a function pointer of type add, uninitialized  

2.2 Typeedef and decltype combine to define function pointer type

typedef decltype(add)* PF2;//PF2 has the same meaning as 1.1PF.  
PF2 pf;// pf points to function pointer of type int(int,int), uninitialized  

2.3 Define function types and function pointers using inferential type keyword auto

auto pf = add;//pf can be considered an alias for 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 type parameter, call automatically converted to function pointer  
void fuc2 (PF2 add);//Function pointer type parameter, pass in corresponding function (pointer)  

Description: Whether the parameter declaration is a function type: void fuc2 (add2 add); or the function pointer type void fuc2 (PF2 add); can be used as a function pointer parameter declaration. When the parameter is passed in, if the function name is passed in, it will automatically be converted into a function pointer.

4 Returns the pointer to the function

4.1 Use auto keyword

auto fuc2(int)-> int(*)(int,int) //The fuc2 return function pointer is int(*)(int,int)

4.2 Use the decltype keyword

decltype(add)* fuc2(int)//Knowing exactly which function to return, decltype keywords can be used to infer its function type.  

5 member function pointer

5.1 Examples of Pointer Use for Common Membership Functions

class A//Define class A  
{  
private:  

   int add(int nLeft, int nRight)  

   {  
          return (nLeft + nRight);  
   }  

public:  

   void fuc()  

   {  
          printf("Hello  world\n");  
         
   }  
};  


typedef void(A::*PF1)();//Class name qualification is required before pointer name  

PF1 pf1 = &A::fuc; //Must have &  

A a;//A member function address dereference must be attached to an object address, so a queue must be created  

(a.*pf1)();//Calling a function with a member function pointer  

5.2 Examples of Function Pointer Use 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)();  //Calling A::fuc  
   (b.*pf1)();   //Calling A::fuc  

   pf1 = &A::fuc2;  
   (a.*pf1)();  //Call A::fuc2  
   (b.*pf1)();  //Call A::fuc2  

   PF2 pf2 = &A::fuc2;   
   (b.*pf2)(); //Call A::fuc2  
}  

Pointer of 6 overloaded functions

6.1 overloaded function fuc

Void fuc();  
Void fuc(int);  

6.2 Function Pointer of Overloaded Function

void (*PF)(int) = fuc;//PF points to fuc(int)  
int(*pf2)(int) = fuc;//Errors do not match the type  

Note: The compiler determines which function to select by pointer type, and the pointer type must match exactly one of the overloaded functions.
Original link: https://www.cnblogs.com/lvchaoshun/p/7806248.html

Posted by Trizen on Fri, 03 May 2019 00:40:37 -0700