std::auto_ptr
为“避免”阵营 提供更多弹药:auto_ptr
在下一个标准(C++0x)中被弃用。我认为仅此一项就足以让任何论点使用其他东西。
但是,正如Konrad Rudolph所提到的,默认替换auto_ptr
可能应该是boost::scoped_ptr
. 的语义与 的语义scoped_ptr
更接近,auto_ptr
并且旨在用于类似的用途。下一个 C++09 标准将有类似的东西,称为 unique_ptr。
However, using shared_ptr
anywhere that scoped_ptr
should be used will not break anything, it'll just add a very slight bit of inefficiency to deal with the reference count if the object is never actually going to be shared. So for private member pointers that will never be handed out to another object - use scoped_ptr
. If the pointer will be handed out to something else (this includes using them in containers or if all you want to do is transfer ownership and not keep or share it) - use shared_ptr
.