2010-07-22 35 views
2

我想我在這裏錯過了一些簡單的東西。我正在使用Boost的shared_ptrC++ shared_ptr - 附加到新的原始指針?

shared_ptr<Foo> pA(new Foo()); 
shared_ptr<Foo> pB(new Foo()); 

現在,我想改用pB所以它包含pA內容,遞減的pB的引用計數。我怎樣才能做到這一點?

+0

「Boost的'的std :: TR1 :: shared_ptr'」 是有點不可思議,難道你不覺得嗎? :) Boost有'boost :: shared_ptr',TR1有'std :: tr1 :: shared_ptr',C++ 0x有'std :: shared_ptr'。 – GManNickG 2010-07-22 04:40:37

+0

Boost還包含一個包含shared_ptr的TR1庫,它將std :: tr1 :: shared_ptr解析爲C++ lib的本地std :: shared_ptr,或者如果不存在boost :: shared_ptr。所以提升的std :: tr1 :: shared_ptr是有效的和平均的。 – 2010-07-22 09:24:18

回答

9

這一切都自動完成:

pB = pA; // pB ref count is decrement (in this case causing the value to be released) 
      // pB is then made to point at the same value as pA 
      // Thus incrementing the refCount. 
相關問題