質問

static_castboost::shared_ptrの同等のものは何ですか?

言い換えれば、次のように書き直す必要があります

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

shared_ptrを使用する場合?

boost::shared_ptr<Base> b(new Derived());
boost::shared_ptr<Derived> d = ???
役に立ちましたか?

解決

boost::static_pointer_castを使用:

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

他のヒント

スマートポインターには、static_pointer_castdynamic_pointer_cast、およびconst_pointer_castの3つのキャスト演算子があります。名前空間boost<boost/shared_ptr.hpp>で提供)または名前空間std::tr1(BoostまたはコンパイラのTR1実装で提供)のいずれかにあります。

コメントとして:Derivedが実際にBaseから派生する場合、静的キャストではなくdynamic_pointer_castを使用する必要があります。システムは、キャストが正しいかどうか/いつかを検出する可能性があります。

ブーストとTR1の実装によって提供されるキャスト演算子の数に違いがあることに言及する価値があります。

TR1は3番目の演算子const_pointer_cast()を定義しません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top