std::shared_ptr<T>::operator=

来自cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
受约束的未初始化内存算法
内存资源
未初始化存储 (C++20 前)
(C++17 弃用)
(C++17 弃用)

垃圾收集器支持 (C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
 
 
shared_ptr& operator=( const shared_ptr& r ) noexcept;
(1)
template< class Y >
shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
(2)
shared_ptr& operator=( shared_ptr&& r ) noexcept;
(3)
template< class Y >
shared_ptr& operator=( shared_ptr<Y>&& r ) noexcept;
(4)
template< class Y >
shared_ptr& operator=( std::auto_ptr<Y>&& r );
(5) (C++11 弃用)
(C++17 移除)
template< class Y, class Deleter >
shared_ptr& operator=( std::unique_ptr<Y, Deleter>&& r );
(6)

r 所管理者替换被管理对象。

*this 已拥有对象且它是最后一个拥有该对象的 shared_ptr,且 r*this 不相同,则通过所拥有的删除器销毁对象。

1,2) 共享 r 所管理对象的所有权。若 r 不管理对象,则 *this 亦不管理对象。等价于 shared_ptr<T>(r).swap(*this)
3,4)r 移动赋值 shared_ptr。赋值后,*this 含有先前 r 状态的副本,而 r 为空,等价于 shared_ptr<T>(std::move(r)).swap(*this)
5) 转移 r 所管理对象的所有权给 *this。若 r 不管理对象,则 *this 亦不管理对象。赋值后,*this 含有先前 r 所保有的对象,且 use_count() == 1;而 r 为空。等价于 shared_ptr<T>(r).swap(*this)
6) 转移 r 所管理对象的所有权给 *this。为将来删除被管理对象,存储 r 所关联的删除器。调用后 r 不管理对象。等价于 shared_ptr<T>(std::move(r)).swap(*this)

目录

[编辑] 参数

r - 要获得所有权或共享所有权的另一智能指针

[编辑] 返回值

*this

[编辑] 注解

实现可以满足要求而无需创建临时的 shared_ptr 对象。

[编辑] 异常

5,6) 可能会抛出由实现定义的异常。

[编辑] 示例

[编辑] 参阅

替换所管理的对象
(公开成员函数) [编辑]