函数指针的几种用法

什么是函数指针?

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

 

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

 

函数指针的声明方法

// 定义函数指针pf

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

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

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

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

 

typedef定义函数指针类型

// 定义函数指针类型cmpFun

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

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

cmpFun pf = 0;

cmpFun pf = someFunction;

 

举个例子来说明一下:

#include <iostream>

#include <string>

using namespace std;

 

// 定义函数指针pf

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

 

// 定义函数指针类型cmpFun

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

 

// 具体函数

int intCompare(const int& aInt, const int& bInt)

{

         if(aInt == bInt) return 0;

         if(aInt > bInt)

         {

                   return 1;

         }

         else

         {

                   return -1;

         }

}

 

int main(void)

{

         int aInt = 1;

         int bInt = 2;

 

         pf = intCompare;

         // pf = &stringCompare;              // 和上面一句是完全一样的

 

         // 使用pf

         if(pf(aInt, bInt) == 0)

         {

                   cout << "two integers are equal" << "." << endl;

         }

         else if(pf(aInt, bInt) > 0)

         {

                   cout << aInt << " is greater than " << bInt << "." << endl;

         }

         else

         {

                   cout << aInt << " is less than " << bInt << "." << endl;

         }

 

         cout << "------------------------" << endl;

         // 用函数指针类型cmpFun声明并初始化一个函数指针pf2

         cmpFun pf2 = intCompare;

         // 使用pf2

         if(pf2(aInt, bInt) == 0)

         {

                   cout << "two integers are equal" << "." << endl;

         }

         else if(pf(aInt, bInt) > 0)

         {

                   cout << aInt << " is greater than " << bInt << "." << endl;

         }

         else

         {

                   cout << aInt << " is less than " << bInt << "." << endl;

         }

 

         return 0;

}

 

 

函数指针作为参数

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

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

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

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

(a)中的plusFun定义如下:

//函数指针作为参数:错误的做法

//int plusFun(int& aInt, int& bInt, int paf(const int& cInt, const int& dInt))

//{

//

//       return aInt + bInt + paf(cInt, dInt);

//}

 

//函数指针作为参数:正确的做法

int plusFun(int& aInt, int& bInt, int paf(const int &, const int &))

{

         int cInt = 2;

         int dInt = 1;

         return aInt + bInt + paf(cInt, dInt);

}

 

调用plusFun的代码:

pf = intCompare;

// 函数指针作为参数

int aaInt = 3;

int bbInt = 4;

cout << plusFun(aaInt, bbInt, pf) << endl;

 

(b)中的plusFun定义如下:

//函数指针作为参数:错误的做法

//int plusFun(int& aInt, int(*paf2)(const int& bInt, const int& cInt))

//{

//       return aInt + paf2(bInt, cInt);

//}

 

//函数指针作为参数:正确的做法

int plusFun(int& aInt, int(*paf2)(const int&, const int&))

{

         int bInt = 1;

         int cInt = 2;

         return aInt + paf2(bInt, cInt);

}

 

调用plusFun的代码:

cmpFun pf2 = intCompare;

// 函数指针作为参数

int aaInt = 3;

cout << plusFun(aaInt, pf2) << endl;

 

 

函数指针作为返回值

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

// 函数指针作为返回值

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

上面的声明的含义:

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

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

 

retFunPointer的定义:

// 函数指针为返回值

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

{

         cout << aInt << endl;

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

         return pf;

}

 

调用代码示例:

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

cmpFun pf3 = retFunPointer(aaInt);

int result = pf3(aaInt, bbInt);

cout << result << endl;

 

 

包含上面所有情况的完整代码

#include <iostream>

#include <string>

using namespace std;

 

// 定义函数指针pf

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

 

// 定义函数指针类型cmpFun

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

 

// 函数指针作为参数

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

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

 

// 函数指针作为返回值

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

 

// 具体函数

int intCompare(const int& aInt, const int& bInt)

{

         if(aInt == bInt) return 0;

         if(aInt > bInt)

         {

                   return 1;

         }

         else

         {

                   return -1;

         }

}

 

//函数指针作为参数:错误的做法

//int plusFun(int& aInt, int& bInt, int paf(const int& cInt, const int& dInt))

//{

//

//       return aInt + bInt + paf(cInt, dInt);

//}

 

//函数指针作为参数:正确的做法

int plusFun(int& aInt, int& bInt, int paf(const int &, const int &))

{

         int cInt = 2;

         int dInt = 1;

         return aInt + bInt + paf(cInt, dInt);

}

 

//函数指针作为参数:错误的做法

//int plusFun(int& aInt, int(*paf2)(const int& bInt, const int& cInt))

//{

//       return aInt + paf2(bInt, cInt);

//}

 

//函数指针作为参数:正确的做法

int plusFun(int& aInt, int(*paf2)(const int&, const int&))

{

         int bInt = 1;

         int cInt = 2;

         return aInt + paf2(bInt, cInt);

}

 

// 函数指针为返回值

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

{

         cout << aInt << endl;

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

         return pf;

}

 

int main(void)

{

         int aInt = 1;

         int bInt = 2;

 

         pf = intCompare;

         // pf = &stringCompare;              // 和上面一句是完全一样的

 

         // 使用pf

         if(pf(aInt, bInt) == 0)

         {

                   cout << "two integers are equal" << "." << endl;

         }

         else if(pf(aInt, bInt) > 0)

         {

                   cout << aInt << " is greater than " << bInt << "." << endl;

         }

         else

         {

                   cout << aInt << " is less than " << bInt << "." << endl;

         }

 

         cout << "------------------------" << endl;

         // 用函数指针类型cmpFun声明并初始化一个函数指针pf2

         cmpFun pf2 = intCompare;

         // 使用pf2

         if(pf2(aInt, bInt) == 0)

         {

                   cout << "two integers are equal" << "." << endl;

         }

         else if(pf(aInt, bInt) > 0)

         {

                   cout << aInt << " is greater than " << bInt << "." << endl;

         }

         else

         {

                   cout << aInt << " is less than " << bInt << "." << endl;

         }

 

         cout << "------------------------" << endl;

         // 函数指针作为参数

         int aaInt = 3;

         int bbInt = 4;

         cout << plusFun(aaInt, bbInt, pf) << endl;

         cout << plusFun(aaInt, pf2) << endl;

 

         cout << "------------------------" << endl;

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

         cmpFun pf3 = retFunPointer(aaInt);

         int result = pf3(aaInt, bbInt);

         cout << result << endl;

 

         return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 函数指针在以下几种情况下常常被使用: 1. 回调函数:当一个函数需要在运行过程中调用另一个函数时,可以使用函数指针作为参数传递给该函数,从而实现回调函数的功能。比如在C++的GUI编程中,常常使用函数指针作为回调函数来处理用户的交互事件。\[2\] 2. 函数指针数组:函数指针可以被存储在数组中,通过数组索引来调用不同的函数。这在一些需要根据不同的条件选择不同函数执行的情况下非常有用。 3. 函数指针作为参数:函数指针可以作为参数传递给其他函数,从而实现函数的动态调用。这在一些需要根据不同的条件选择不同函数执行的情况下非常有用。 4. 函数指针作为返回值:函数指针也可以作为函数的返回值,从而实现根据不同的条件返回不同的函数。这在一些需要根据不同的条件选择不同函数执行的情况下非常有用。 总之,函数指针在需要动态调用函数、实现回调函数、根据不同条件选择不同函数执行等情况下非常有用。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [函数指针用法以及用途详解](https://blog.csdn.net/m0_62755690/article/details/127318245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值