提升shared_ptr和派生类

时间:2013-10-19 13:31:05

标签: c++ boost shared-ptr

我有这样的代码:

class Base { ... };

class Derived : public Base
{ ... };

boost:shared_ptr<Base> p;

int main()
{
  p(new Derived);
 ...
}

在我看来,这不起作用。 我错过了什么?

1 个答案:

答案 0 :(得分:2)

你在课堂初始化和作业混淆时会感到困惑。成员初始化列表语法仅在类主体内部起作用:

p = boost::make_shared<Derived>();
相关问题