简介
C++11 提出bind 实际上是为了解决 函数参数的不同的问题. 然后绑定赋值给function<> 函数封装器. 然后可以通过function<> 绑定的对象进行函数调用.
参考链接
https://zhuanlan.zhihu.com/p/55924014
code
#include <iostream>
#include <functional>
class A{
std::function<void()> m_callback;
public:
A(const std::function<void()>& f): m_callback(f) {}
void notify(){
m_callback();
}
};
class Foo{
public:
void print() {
std::cout << "__FUNCTION__" << std::endl;
}
};
void printtt() {
std::cout << "__FUNCTION__" << std::endl;
}
int main() {
Foo foo;
std::function<void()> fr = std::bind(&Foo::print, foo);
A aa(fr);
aa.notify();
}