我在这里了解到std::auto_ptr<>::operator=的相关内容。
注意,左侧对象在指向某个对象时不会自动释放。您可以在分配新值之前通过调用成员函数reset来显式执行此操作。
然而,当我查看头文件C:\Program Files\Microsoft Visual Studio 8\VC\ce\include\memory
的源代码时:
template<class _Other>
auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // assign compatible _Right._Ref (assume pointer)
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0; // release old
reset(_Ptr); // set new
return (*this);
}
什么是正确/标准的行为?其他STL实现如何行事?如果上述网站有错误/过时的信息,您推荐哪个网站作为参考?
auto_ptr
。相反,应该使用boost::scoped_ptr
或boost::shared_ptr
。 - rlbond