函数指针

转自:函数指针

什么是函数指针?

函数指针指向的是特殊的数据类型,函数的类型是由其返回的数据类型和其参数列表共同决定的,而函数的名称则不是其类型的一部分。

一个具体函数的名字,如果后面不跟调用符号(即括号),则该名字就是该函数的指针(注意:大部分情况下,可以这么认为,但这种说法并不很严格)。

函数指针的声明方法

// 定义函数指针pf

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

上面的pf就是一个函数指针,指向所有返回类型为int,并带有两个const int&参数的函数。注意*pf两边的括号是必须的,否则上面的定义就变成了:

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

而这声明了一个函数pf,其返回类型为int *, 带有两个const int&参数。

用typedef定义函数指针类型

// 定义函数指针类型cmpFun

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

这样,cmpFun就成了一种数据类型,可以用它来声明和定义形如(1)式中的pf那样的函数指针,比如:

cmpFun pf = 0;

cmpFun pf = someFunction;

举个例子来说明一下:

  1. #include <iostream>  
  2.   
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7.    
  8.   
  9. // 定义函数指针pf  
  10.   
  11. int (*pf)(const int&, const int&);  
  12.   
  13.    
  14.   
  15. // 定义函数指针类型cmpFun  
  16.   
  17. typedef int (*cmpFun)(const int&, const int&);  
  18.   
  19.    
  20.   
  21. // 具体函数  
  22.   
  23. int intCompare(const int& aInt, const int& 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;              // 和上面一句是完全一样的  
  62.   
  63.    
  64.   
  65.          // 使用pf  
  66.   
  67.          if(pf(aInt, bInt) == 0)  
  68.   
  69.          {  
  70.   
  71.                    cout << "two integers are equal" << "." << endl;  
  72.   
  73.          }  
  74.   
  75.          else if(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.          // 用函数指针类型cmpFun声明并初始化一个函数指针pf2  
  96.   
  97.          cmpFun pf2 = intCompare;  
  98.   
  99.          // 使用pf2  
  100.   
  101.          if(pf2(aInt, bInt) == 0)  
  102.   
  103.          {  
  104.   
  105.                    cout << "two integers are equal" << "." << endl;  
  106.   
  107.          }  
  108.   
  109.          else if(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. }  

函数指针作为参数

函数指针可以作为一个函数的参数,如下两种办法可以做到这一点:

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

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

以上两个方式做到的是类似的事情:(a)中的plusFun函数的第三个参数就是一个函数指针, (b)中的第二个参数也是一个函数指针。下面我们分别定义前面声明的两个plusFun函数。

(a)中的plusFun定义如下:

  1. //函数指针作为参数:错误的做法  
  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. //函数指针作为参数:正确的做法  
  16.   
  17. int plusFun(int& aInt, int& bInt, int paf(const int &, const int &))  
  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. 调用plusFun的代码:  
  32.   
  33. …  
  34.   
  35. pf = intCompare;  
  36.   
  37. …  
  38.   
  39. // 函数指针作为参数  
  40.   
  41. int aaInt = 3;  
  42.   
  43. int bbInt = 4;  
  44.   
  45. cout << plusFun(aaInt, bbInt, pf) << endl;  
  46.   
  47.    
  48.   
  49. (b)中的plusFun定义如下:  
  50.   
  51. //函数指针作为参数:错误的做法  
  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. //函数指针作为参数:正确的做法  
  64.   
  65. int plusFun(int& aInt, int(*paf2)(const int&, const int&))  
  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. 调用plusFun的代码:  
  80.   
  81. …  
  82.   
  83. cmpFun pf2 = intCompare;  
  84.   
  85. …  
  86.   
  87. // 函数指针作为参数  
  88.   
  89. int aaInt = 3;  
  90.   
  91. cout << plusFun(aaInt, pf2) << endl;  

函数指针作为返回值

一个函数的返回值可以是一个函数指针,这个声明形式写起来有点麻烦:

// 函数指针作为返回值

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

上面的声明的含义:

a) retFunPointer是一个函数,该函数有一个int类型的参数;

b) retFunPointer返回值是一个函数指针,它指向的是带有两个const int&类型参数,且返回类型为int的函数。

retFunPointer的定义:

// 函数指针为返回值

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

{

cout << aInt << endl;

// pf已经在前面定义过了

return pf;

}

调用代码示例:

// 函数指针作为返回值,retFunPointer返回一个cmpFun类型的函数指针

cmpFun pf3 = retFunPointer(aaInt);

int result = pf3(aaInt, bbInt);

cout << result << endl;

猜你喜欢

转载自blog.csdn.net/wu_cai_/article/details/80056137