有一些标准系统函数(在示例中Job_test
)调用带有参数的回调函数。我想把回调函数本身作为类的一部分,然后用std::bind
它来用一个参数调用它this
下面的示例编译正常,但由于编译错误do_job2
而失败:do_job1
不存在从“std::_Bind (ca *)>”到“ArCallBackFunction1”的合适的用户定义转换
不同的是,它do_job1
调用callback时带一个参数,do_job2
调用callback时不带参数。
请告诉我如何写一个例子do_job1
?
typedef std::function<void(int a)> ArCallBackFunction1;
typedef std::function<void()> ArCallBackFunction2;
void Job_test1( ArCallBackFunction1 onRequest) { onRequest(2); };
void Job_test2( ArCallBackFunction2 onRequest) { onRequest(); };
class ca {
public:
void callBack1(int a) { Serial.println("a"); };
void callBack2() { Serial.println("a"); };
void do_job1() {
Job_test1( std::bind(&ca::callBack1,this));
}
void do_job2() {
Job_test2( std::bind(&ca::callBack2,this));
}
};
标准::占位符