如何在Visual Studio 2017上将类方法设置为参数并将其与lambda一起使用?

问题描述 投票:0回答:1

我在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表达式一起使用。

我如何:

  1. 制作类方法的指针?
  2. 发送给另一种方法?
  3. 是否在lambda函数中运行?

如果能得到一些示例代码,我将不胜感激。

c++ pointers methods mfc
1个回答
0
投票

这里是对第一季度的尝试:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.