[英]Do boost::shared_ptr<T> and boost::shared_ptr<const T> share the reference count?
[英]boost::shared_ptr<const T> to boost::shared_ptr<T>
我想從boost::shared_ptr
拋出const-ness,但是我的boost::const_pointer_cast
不是答案。 boost::const_pointer_cast
想要一個const boost::shared_ptr<T>
,而不是一個boost::shared_ptr<const T>
。 讓我們放棄強制性的“你不應該這樣做”。 我知道......但我需要這樣做......那么最好/最簡單的方法是什么?
為清楚起見:
boost::shared_ptr<const T> orig_ptr( new T() );
boost::shared_ptr<T> new_ptr = magic_incantation(orig_ptr);
我需要知道magic_incantation()
boost::const_pointer_cast
是你想要使用的函數:
boost::shared_ptr<const int> ci(new int(42));
boost::shared_ptr<int> i(boost::const_pointer_cast<int>(ci));
這不適合你嗎? 我使用Boost 1.43和Visual C ++ 2010 C ++ 0x實現進行了測試 - 兩者都沒有問題。
請注意,至少可以說,如果共享的const T
突然改變,其他“股東”會非常驚訝 ......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.