可能重复:
std::auto_ptr to std::unique_ptr
What C++ Smart Pointer Implementations are available?的unique_ptr VS auto_ptr的
可以说我有这个struct
:
struct bar
{
};
当我使用auto_ptr的这样的:
void foo()
{
auto_ptr<bar> myFirstBar = new bar;
if()
{
auto_ptr<bar> mySecondBar = myFirstBar;
}
}
然后在auto_ptr<bar> mySecondBar = myFirstBar;
C++传送从myFirstBar到mySecondBar所有权并且没有编译错误。
但是,当我使用unique_ptr而不是auto_ptr我得到一个编译器错误。为什么C++不允许这样做?这两个智能指针的主要区别是什么?当我需要使用什么?
见http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available – hmjd