Search code examples
c++boostshared-ptr

boost shared_ptr and derived class


I have such a code:

class Base { ... };

class Derived : public Base
{ ... };

boost:shared_ptr<Base> p;

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

It seems to me that this isn't working. What am I missing?


Solution

  • You're confusing in class initialization with assignment. The member-initializer list syntax only works inside of the class body:

    p = boost::make_shared<Derived>();