【发布时间】:2016-03-24 13:59:43
【问题描述】:
在创建调用成员函数的线程时,传递指向当前类的指针和传递引用有区别吗?
从下面的示例中,method1 的行为是否与 method2 相同?有什么不同吗?
class MyClass
{
public:
MyClass(){};
~MyClass(){};
void memberFunction1()
{
//method 1
std::thread theThread(&MyClass::memberFunction2, this, argumentToMemberFunction2)
//method 2
std::thread theThread(&MyClass::memberFunction2, std::ref(*this), argumentToMemberFunction2)
}
void memberFunction2(const double& someDouble){};
}
【问题讨论】:
-
假设它编译,不应该有。引用是指针的语法糖。但这并不意味着它不能搞砸编译器,因为当调用(内部)以
this作为指针作为第一个对象时传递std::refreference_wrapper在语法上是可疑的。
标签: c++ multithreading pointers pass-by-reference member-functions