C++函数指针是指向函数的指针变量,它存储的是函数在内存中的入口地址,通过函数指针可以间接调用对应的函数。函数指针在C++开发中有多种实用场景,合理使用能提升代码的灵活性和可扩展性。
函数指针的基础定义
函数指针的定义需要匹配目标函数的返回值类型和参数列表,基本语法为返回值类型 (*指针名)(参数类型列表)。下面是一个简单的函数指针定义示例:
#include <iostream>
using namespace std;
// 定义一个普通函数,返回两数之和
int add(int a, int b) {
return a + b;
}
// 定义一个普通函数,返回两数之差
int sub(int a, int b) {
return a - b;
}
int main() {
// 定义函数指针,指向返回值为int、参数为两个int的函数
int (*func_ptr)(int, int);
// 将add函数的地址赋值给函数指针
func_ptr = add;
// 通过函数指针调用add函数
cout << "add result: " << func_ptr(3, 2) << endl; // 输出5
// 将sub函数的地址赋值给函数指针
func_ptr = sub;
// 通过函数指针调用sub函数
cout << "sub result: " << func_ptr(3, 2) << endl; // 输出1
return 0;
}
核心用途一:实现回调函数
回调函数是指将一个函数作为参数传递给另一个函数,在合适时机被调用的函数,函数指针是实现回调的核心方式。比如排序算法中,我们可以传入不同的比较函数来实现升序或降序排序。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 升序比较函数
bool cmp_asc(int a, int b) {
return a < b;
}
// 降序比较函数
bool cmp_desc(int a, int b) {
return a > b;
}
// 接收函数指针作为参数的排序函数
void custom_sort(vector<int>& arr, bool (*cmp)(int, int)) {
// 使用传入的比较函数进行排序
sort(arr.begin(), arr.end(), cmp);
}
int main() {
vector<int> nums = {5, 2, 8, 1, 9};
// 传入升序比较函数
custom_sort(nums, cmp_asc);
cout << "升序结果: ";
for (int num : nums) {
cout << num << " ";
}
cout << endl; // 输出1 2 5 8 9
// 传入降序比较函数
custom_sort(nums, cmp_desc);
cout << "降序结果: ";
for (int num : nums) {
cout << num << " ";
}
cout << endl; // 输出9 8 5 2 1
return 0;
}
核心用途二:实现策略模式
策略模式需要定义一系列算法,并且让它们可以相互替换,函数指针可以很方便地实现这种动态策略切换,不需要定义复杂的类结构。
#include <iostream>
using namespace std;
// 策略1:计算普通折扣,满100减20
double discount_normal(double price) {
if (price >= 100) {
return price - 20;
}
return price;
}
// 策略2:计算会员折扣,打8折
double discount_vip(double price) {
return price * 0.8;
}
// 策略3:计算超级会员折扣,打6折
double discount_svip(double price) {
return price * 0.6;
}
// 结账函数,接收折扣策略函数指针
double checkout(double price, double (*discount_strategy)(double)) {
return discount_strategy(price);
}
int main() {
double original_price = 150.0;
cout << "普通用户结账: " << checkout(original_price, discount_normal) << endl; // 输出130
cout << "会员用户结账: " << checkout(original_price, discount_vip) << endl; // 输出120
cout << "超级会员结账: " << checkout(original_price, discount_svip) << endl; // 输出90
return 0;
}
核心用途三:轻量多态实现
虚函数是C++实现多态的常用方式,但虚函数会带来一定的性能开销,函数指针可以在不需要复杂类继承的场景下实现轻量多态,减少额外开销。
#include <iostream>
#include <vector>
using namespace std;
// 定义不同形状的绘制函数
void draw_circle() {
cout << "绘制圆形" << endl;
}
void draw_rectangle() {
cout << "绘制矩形" << endl;
}
void draw_triangle() {
cout << "绘制三角形" << endl;
}
int main() {
// 存储不同绘制函数的函数指针数组
vector<void (*)()> draw_funcs;
draw_funcs.push_back(draw_circle);
draw_funcs.push_back(draw_rectangle);
draw_funcs.push_back(draw_triangle);
// 遍历数组调用不同的绘制函数,实现多态效果
for (auto func : draw_funcs) {
func();
}
return 0;
}
核心用途四:动态选择执行逻辑
在需要根据运行时条件选择不同函数执行的场景下,函数指针可以避免大量的if-else或者switch-case判断,让代码更简洁。
#include <iostream>
#include <map>
#include <string>
using namespace std;
// 定义不同的操作函数
int op_add(int a, int b) { return a + b; }
int op_sub(int a, int b) { return a - b; }
int op_mul(int a, int b) { return a * b; }
int op_div(int a, int b) { return b != 0 ? a / b : 0; }
int main() {
// 建立操作名到函数指针的映射
map<string, int (*)(int, int)> op_map;
op_map["add"] = op_add;
op_map["sub"] = op_sub;
op_map["mul"] = op_mul;
op_map["div"] = op_div;
string op;
int a, b;
cout << "请输入操作名和两个数字: ";
cin >> op >> a >> b;
// 动态查找对应的函数指针并执行
if (op_map.find(op) != op_map.end()) {
cout << "结果: " << op_map[op](a, b) << endl;
} else {
cout << "不支持的操作" << endl;
}
return 0;
}
使用注意事项
- 函数指针的类型必须和指向的函数完全匹配,包括返回值类型和参数类型、数量,否则会导致编译错误。
- 函数指针可以赋值为nullptr,调用前需要判断指针是否有效,避免空指针调用导致程序崩溃。
- 对于复杂的函数指针类型,可以使用typedef或者using来定义别名,提升代码可读性,比如
using CalcFunc = int (*)(int, int);。 - 现代C++中也可以使用std::function来替代部分函数指针场景,但函数指针在和C语言交互、追求极致性能的场景下仍有不可替代的作用。