std::auto_ptr<>::operator = 重置/取消分配其现有指针?

发布于 2024-08-27 16:01:03 字数 1139 浏览 6 评论 0 原文

我在这里读到了关于std::auto_ptr的内容: :运算符=

但是请注意,左侧 对象不会自动 当它已经指向时释放 某个对象。你可以明确地做 通过调用成员函数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 实现的行为如何?如果上述网站有错误/过时的信息,您推荐哪个网站作为参考?

I read here about std::auto_ptr<>::operator=

Notice however that the left-hand side
object is not automatically
deallocated when it already points to
some object. You can explicitly do
this by calling member function reset
before assigning it a new value.

However, when I read the source code for header file 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);
    }

What is the correct/standard behavior? How do other STL implementations behave? If the website cited above has wrong/outdated information, which website do you recommend as a reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-09-03 16:01:03

如果被分配的 auto_ptr 已经持有一个指针,则必须首先删除该指针。

摘自 2003 年标准 (§20.4.5.1):

auto_ptr& operator=(auto_ptr& a) throw();

7 要求:表达式 delete get() 格式正确。

8 个效果:重置(a.release())

9 返回:*this

因此,分配给 auto_ptr 与通过从右​​侧 auto_ptr 释放指针来对其调用 reset 具有相同的效果。

你引用的网站是错误的。

If the auto_ptr being assigned to already holds a pointer, that pointer must be deleted first.

From the 2003 standard (§20.4.5.1):

auto_ptr& operator=(auto_ptr& a) throw();

7 Requires: The expression delete get() is well formed.

8 Effects: reset(a.release()).

9 Returns: *this.

So, assigning to an auto_ptr has the same effect as calling reset on it with the pointer released from the right hand side auto_ptr.

The website you cite is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文