利用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:映射时允许出现非满射的情况,及适配函数有一个参数,但是被适配函数中有两个参数