c++ 用std::function包装类的非静态成员

利用std::bind();

std::bind()类似函数之间的适配器,可以将封装不同的,但是可转换的函数指针的function转换;

这里我们不探讨在函数适配上的功能;

直接上代码,看代码理解比文字好一百倍:

#include<iostream>
#include<functional>
#include<string>
using namespace std;

class testClass {
public:
	void Callback(int Integer, std::string str) {
		std::cout << Integer << ' ' << str << std::endl;
	}
};

void main() {
	testClass testInstance;
	std::function<void(double, std::string)> callback = std::bind(&testClass::Callback, &testInstance, std::placeholders::_1, std::placeholders::_2);
	callback(1, "2022");
}

这里简单说一下std::bind的特性:

1:返回值可以直接转换为std::function<>

2:当适配器对接的两个参数之间有转换机制时,可以通过

3:映射时允许出现非满射的情况,及适配函数有一个参数,但是被适配函数中有两个参数

总之,适配时只要不违法“信息不会凭空产生”这个原则即可;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值