class t
{
public:
vector<shared_ptr<thread> > t1;
public:
t()
{
for (int i = 0; i < 3; i++)
{
t1.push_back(make_shared<thread>(&t::Func, this)); //(1)
}
}
void Func()
{
cout << " hello world!" << endl;
}
};
请问为什么在(1)的地方一定要如此初始化呢,
不写成这样他的报错会是:
t。错误:ISO c++禁止使用非限定或圆括号的非静态成员函数的地址来形成成员函数的指针
是因为禁止直接将非静态的类成员函数 t::Func
直接转换到thread
类指针么?,
如果是因为这样那为什么一定要这样写呢? 像&(t::Func)
也会报上面的错
t::Func
是t
的成员函数,有一个隐藏参数this
,自然要先传一个参数进去。。t1.push_back(make_shared<thread>(&t::Func, NULL)); //(1)
你这么写也行。。