Search code examples
c++boostshared-ptrstatic-cast

static_cast with boost::shared_ptr?


What is the equivalent of a static_cast with boost::shared_ptr?

In other words, how do I have to rewrite the following

Base* b = new Derived();
Derived* d = static_cast<Derived*>(b);

when using shared_ptr?

boost::shared_ptr<Base> b(new Derived());
boost::shared_ptr<Derived> d = ???

Solution

  • Use boost::static_pointer_cast:

    boost::shared_ptr<Base> b(new Derived());
    boost::shared_ptr<Derived> d = boost::static_pointer_cast<Derived>(b);