我知道这个问题会被标记为重复,因为我也读过几个类似的问题。但不幸的是,没有一个答案对我有用。我尝试了所有这些,作为最后一个选择,我想问一下。
void AsyncClass::method1()
{
cout << "method is called" << endl;
}
void AsyncClass::method2()
{
auto t = new std::thread(this->method1);
}
这两个方法都是公共(public)的且非静态的。这不会编译说
non-standard syntax; use '&' to create a pointer to member
同时考虑我尝试过的答案
auto t = new std::thread(this->method1);
auto t = new std::thread(this->*method1);
auto t = new std::thread(&(this->method1));
auto t = new std::thread(&AsyncClass::method1);
它们都没有编译。正确的方法是什么?
最佳答案
你应该这样做:
auto t = new std::thread(&AsyncClass::method1, this);
关于c++ - 如何获取指向成员函数的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33763011/