为了账号安全,请及时绑定邮箱和手机立即绑定

c++11shared_ptr存储线程

c++11shared_ptr存储线程

C++
隔江千里 2018-07-23 21:56:06
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)也会报上面的错
查看完整描述

1 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

t::Func 是t的成员函数,有一个隐藏参数this,自然要先传一个参数进去。。
t1.push_back(make_shared<thread>(&t::Func, NULL)); //(1) 你这么写也行。。

查看完整回答
反对 回复 2018-07-29
  • 1 回答
  • 0 关注
  • 903 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信