命名空间
变体
操作

std::shared_ptr<T>::operator=

来自 cppreference.cn
< cpp‎ | 内存‎ | shared ptr
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
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)

目录

[edit] 参数

r - 另一个智能指针,用于共享所有权或从中获取所有权

[edit] 返回值

*this

[edit] 注意

实现可能无需创建临时 shared_ptr 对象即可满足要求。

[edit] 异常

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

[edit] 示例

[edit] 另请参阅

替换托管对象
(public member function) [edit]