function pointer

From: function pointers

What is a function pointer?

A function pointer points to a special data type, the type of the function is determined by the data type it returns and its parameter list, and the name of the function is not part of its type.

The name of a concrete function, if not followed by a call symbol (that is, parentheses), then the name is a pointer to the function (note: in most cases, this can be considered, but it is not very strict).

How to declare function pointer

// Define the function pointer pf

int (*pf)(constint&, const int&); (1)

The above pf is a function pointer that points to all functions whose return type is int and takes two const int& parameters. Note that the parentheses around *pf are required, otherwise the above definition becomes:

int *pf(const int&, const int&); (2)

And this declares a function pf with return type int * with two const int& parameters.

Defining function pointer types with typedef

// Define the function pointer type cmpFun

typedef int (*cmpFun)(constint&, const int&); (3)

In this way, cmpFun becomes a data type that can be used to declare and define function pointers in the form of pf in (1), for example:

cmpFun pf = 0;

cmpFun pf = someFunction;

Take an example to illustrate:

  1. #include <iostream>  
  2.   
  3. #include <string>  
  4.   
  5. usingnamespace std;   
  6.   
  7.    
  8.   
  9. // define the function pointer pf  
  10.   
  11. int (*pf)(constint&, constint&);    
  12.   
  13.    
  14.   
  15. // Define the function pointer type cmpFun  
  16.   
  17. typedefint (*cmpFun)(constint&, constint&);     
  18.   
  19.    
  20.   
  21. // concrete function  
  22.   
  23. int intCompare(constint& aInt, constint& bInt)    
  24.   
  25. {  
  26.   
  27.          if(aInt == bInt) return 0;  
  28.   
  29.          if(aInt > bInt)   
  30.   
  31.          {  
  32.   
  33.                    return 1;  
  34.   
  35.          }  
  36.   
  37.          else  
  38.   
  39.          {  
  40.   
  41.                    return -1;  
  42.   
  43.          }  
  44.   
  45. }  
  46.   
  47.    
  48.   
  49. int main(void)  
  50.   
  51. {  
  52.   
  53.          int aInt = 1;  
  54.   
  55.          int  bInt = 2;  
  56.   
  57.    
  58.   
  59.          pf = intCompare;  
  60.   
  61.          // pf = &stringCompare; // exactly the same as above  
  62.   
  63.    
  64.   
  65.          // use pf  
  66.   
  67.          if(pf(aInt, bInt) == 0)  
  68.   
  69.          {  
  70.   
  71.                    cout << "two integers are equal" << "." << endl;  
  72.   
  73.          }  
  74.   
  75.          elseif(pf(aInt, bInt) > 0)   
  76.   
  77.          {  
  78.   
  79.                    cout << aInt << " is greater than " << bInt << "." << endl;  
  80.   
  81.          }  
  82.   
  83.          else  
  84.   
  85.          {  
  86.   
  87.                    cout << aInt << " is less than " << bInt << "." << endl;  
  88.   
  89.          }  
  90.   
  91.    
  92.   
  93.          cout << "------------------------" << endl;  
  94.   
  95.          // Declare and initialize a function pointer pf2 with function pointer type cmpFun  
  96.   
  97.          cmpFun pf2 = intCompare;  
  98.   
  99.          // use pf2  
  100.   
  101.          if(pf2(aInt, bInt) == 0)  
  102.   
  103.          {  
  104.   
  105.                    cout << "two integers are equal" << "." << endl;  
  106.   
  107.          }  
  108.   
  109.          elseif(pf(aInt, bInt) > 0)   
  110.   
  111.          {  
  112.   
  113.                    cout << aInt << " is greater than " << bInt << "." << endl;  
  114.   
  115.          }  
  116.   
  117.          else  
  118.   
  119.          {  
  120.   
  121.                    cout << aInt << " is less than " << bInt << "." << endl;  
  122.   
  123.          }  
  124.   
  125.    
  126.   
  127.          return 0;  
  128.   
  129. }  

function pointer as parameter

A function pointer can be used as a parameter to a function. There are two ways to do this:

(a) int plusFun(int&,int&, int (constint&, const int&));

(b) int plusFun(int&,int(*)(const int&, const int&));

The above two methods do similar things: the third parameter of the plusFun function in (a) is a function pointer, and the second parameter in (b) is also a function pointer. Below we define the two plusFun functions declared earlier.

The plusFun in (a) is defined as follows:

  1. // function pointer as parameter: bad practice  
  2.   
  3. //int plusFun(int& aInt, int& bInt, int paf(const int& cInt, const int& dInt))  
  4.   
  5. //{  
  6.   
  7. //  
  8.   
  9. // return aInt + bInt + paf (cInt, dInt);  
  10.   
  11. //}  
  12.   
  13.    
  14.   
  15. // function pointer as parameter: the right way to do it  
  16.   
  17. int plusFun(int& aInt, int& bInt, int paf(constint &, constint &))    
  18.   
  19. {  
  20.   
  21.          int  cInt = 2;  
  22.   
  23.          int  dInt = 1;  
  24.   
  25.          return  aInt + bInt + paf (cInt, dInt);  
  26.   
  27. }  
  28.   
  29.    
  30.   
  31. The code to call plusFun:  
  32.   
  33. …  
  34.   
  35. pf = intCompare;  
  36.   
  37. …  
  38.   
  39. // function pointer as parameter  
  40.   
  41. int aaInt = 3;  
  42.   
  43. int bbInt = 4;  
  44.   
  45. cout << plusFun(aaInt, bbInt, pf) << endl;  
  46.   
  47.    
  48.   
  49. The plusFun in (b) is defined as follows:  
  50.   
  51. // function pointer as parameter: bad practice  
  52.   
  53. //int plusFun(int& aInt, int(*paf2)(const int& bInt, const int& cInt))  
  54.   
  55. //{  
  56.   
  57. //       return aInt + paf2(bInt, cInt);  
  58.   
  59. //}  
  60.   
  61.    
  62.   
  63. // function pointer as parameter: the right way to do it  
  64.   
  65. int plusFun(int& aInt, int(*paf2)(constint&, constint&))    
  66.   
  67. {  
  68.   
  69.          int  bInt = 1;  
  70.   
  71.          int  cInt = 2;  
  72.   
  73.          return aInt + paf2(bInt, cInt);  
  74.   
  75. }  
  76.   
  77.    
  78.   
  79. The code to call plusFun:  
  80.   
  81. …  
  82.   
  83. cmpFun pf2 = intCompare;  
  84.   
  85. …  
  86.   
  87. // function pointer as parameter  
  88.   
  89. int aaInt = 3;  
  90.   
  91. cout << plusFun(aaInt, pf2) << endl;  

function pointer as return value

The return value of a function can be a function pointer. This declaration form is a bit cumbersome to write:

// function pointer as return value

int (*retFunPointer(int))(constint&, const int&);

The meaning of the above statement:

a) retFunPointer is a function that has a parameter of type int;

b) The return value of retFunPointer is a function pointer, which points to a function with two const int& type parameters and the return type is int.

Definition of retFunPointer:

// function pointer is the return value

int (*retFunPointer(int aInt))(constint&, const int&)

{

cout << aInt << endl;

// pf has been defined earlier

return pf;

}

Example of calling code:

// The function pointer is used as the return value, retFunPointer returns a function pointer of type cmpFun

cmpFun pf3 = retFunPointer(aaInt);

int result = pf3(aaInt, bbInt);

cout << result << endl;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724829&siteId=291194637