我在Visual Studio 2017中使用C ++ 17。我想使用lambda表达式在另一个类方法中执行一个类方法。到目前为止,我已经这样完成过:
void CMFCApplicationDlg::Add_text() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app);
outfile << "text added" << std::endl;
}
void CMFCApplicationDlg::Start_adding() {
sched.cron("0 12 * * *", [this]() { CMFCApplicationDlg::Add_text(); });
}
[我认为最好将Add_text
的指针发送到Start_adding
作为参数,并将其与lambda表达式一起使用。
我如何:
如果能得到一些示例代码,我将不胜感激。
这里是对第一季度的尝试:
class foo
{
std::function<void(void)> func = std::bind(&foo::func1, this);
std::function<void(void)>* func_ptr = &func;
void func1(void) { cout << " Its foo \n"; }
public:
void func2() {
(*func_ptr)();
}
};
int main()
{
foo f;
f.func2();
}