[c++]关于c++11的function函数的使用:

C++11 引入了一个叫做 std::function 的函数封装器,它可以存储、复制和调用任何可调用对象(如函数、Lambda 表达式、成员函数指针等)。

function的基本用法:

#include<iostream>
#include<functional>
#include<string>
#include<algorithm>
using namespace std;
void Func(const string str) {
	cout << str + str << endl;
}
class Base {
	string str;
	int a;
public:
	Base(string str) {
		this->str = str;
	}
	static int StaticFunc(int val) {
		cout << val << endl;
		return val;
	}
	int func(int val) {
		cout <<"Base: "<< str<<" "<<val << endl;
		this->a = val;
		return a;
	}
};
class Functor {
public:
	void operator()(const string str, int val) {
		cout << str << " " << val << endl;
	}
};
int main() {
	//function封装普通函数
	std::function<void(const string)>funA(Func);
	funA("abc");
	//封装静态成员函数
	std::function<int(int)>funB(Base::StaticFunc);
	funB(20);
	//封装仿函数
	std::function<void(const string, int)>funC = Functor();
	funC("abc", 20);
	//绑定lambda表达式
	std::function<void(int)>funD = [](int val) {
		cout << "lambda: " << val << endl;
	};
	funD(30);
	//绑定类的成员函数
	//当function绑定类的成员函数时需要先实例化对象
	function<int(Base*, int)>funE(&Base::func);
	Base objA("aaa");
	Base objB("bbb");
	funE(&objA, 20);
	funE(&objB, 30);


}

使用function方便实现回调函数:

使用 std::function 可以方便地实现回调函数、事件处理等功能。下面是一个简单的例子,展示了如何使用 std::function 实现简单的回调函数:

#include<iostream>
#include<functional>
#include<string>
#include<algorithm>
using namespace std;
void Func(const string str) {
	cout << str + str << endl;
}
class Base {
	string str;
	int a;
public:
	Base(string str) {
		this->str = str;
	}
	static int StaticFunc(int val) {
		cout << val << endl;
		return val;
	}
	int func(int val) {
		cout <<"Base: "<< str<<" "<<val << endl;
		this->a = val;
		return a;
	}
};
class Functor {
public:
	void operator()(const string str, int val) {
		cout << str << " " << val << endl;
	}
};
int main() {
	//function封装普通函数
	std::function<void(const string)>funA(Func);
	funA("abc");
	//封装静态成员函数
	std::function<int(int)>funB(Base::StaticFunc);
	funB(20);
	//封装仿函数
	std::function<void(const string, int)>funC = Functor();
	funC("abc", 20);
	//绑定lambda表达式
	std::function<void(int)>funD = [](int val) {
		cout << "lambda: " << val << endl;
	};
	funD(30);
	//绑定类的成员函数
	//当function绑定类的成员函数时需要先实例化对象
	function<int(Base*, int)>funE(&Base::func);
	Base objA("aaa");
	Base objB("bbb");
	funE(&objA, 20);
	funE(&objB, 30);


}

使用function的好处:

  • 灵活性: std::function提供了一种通用的封装,能够接受不同类型的可调用对象。这种灵活性使得我们可以轻松地将函数、函数指针、Lambda 表达式等作为参数传递给函数或者存储在容器中。

  • 可扩展性: 使用std::function使得代码更具可扩展性,因为我们可以在运行时动态选择要调用的函数或者函数对象,从而实现更加灵活的逻辑和控制流。

  • 复用性: 函数对象可以被多次调用,可以在不同的上下文中复用。这种特性在需要多次调用相同逻辑的情况下非常有用。

  • 回调: std::function可用于实现回调机制,例如事件处理、消息传递等场景。通过指定std::function来处理某个事件,可以更方便地实现模块之间的通信。

  • 对象适配: 有时候我们需要将函数或函数对象转换成不同类型的函数对象,std::function提供了这种适配的机制,使得不同类型的函数对象可以协同工作。

综上所述,使用std::function可以使代码更加灵活、可扩展、可复用,并且提供了回调和对象适配等重要功能,从而提高了C++程序的可维护性和可扩展性。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值