使用bind绑定到成员函数时,即使成员函数不需参数,也要将this绑定在第一个参数:
#include <iostream>
#include <functional>
using namespace std;
class test {
public:
test() : function(std::bind(&test::func, this, 3)) { } // 需将this绑定在func的第一个参数处
function<void()> function;
private:
void func(int i) {
cout << "in function, the parameter is: " << i << endl;
}
};
int main() {
test t;
t.function();
}
运行它: