我polymorphic_cast
对普通指针的提升很熟悉:
Base *base;
Derived *d = boost::polymorphic_cast<Derived>(base);
但是,如何使用它boost::shared_ptr
呢?
boost::shared_ptr<Base> base;
boost::shared_ptr<Derived> d = boost::?????(base);
我polymorphic_cast
对普通指针的提升很熟悉:
Base *base;
Derived *d = boost::polymorphic_cast<Derived>(base);
但是,如何使用它boost::shared_ptr
呢?
boost::shared_ptr<Base> base;
boost::shared_ptr<Derived> d = boost::?????(base);
使用boost::static_pointer_cast
or boost::dynamic_pointer_cast
,作为 C++ 强制转换的类似物static_cast
和dynamic_cast
:
boost::shared_ptr<Derived> d = boost::static_pointer_cast<Derived>(base);
// now "d" shares ownership with "base"
这只是对底层原始指针执行相应的转换。
(std
C++11 标准库中的std::tr1
命名空间和 C++03 的 TR1 库中的命名空间也是如此。)