命名空间
变体
操作

std::shared_ptr<T>::operator=

来自 cppreference.cn
< cpp‎ | memory‎ | 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)

内容

[编辑] 参数

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

[编辑] 返回值

*this

[编辑] 注意

该实现可能在不创建临时 shared_ptr 对象的情况下满足要求。

[编辑] 异常

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

[编辑] 示例

[编辑] 参见

替换托管对象
(公共成员函数) [编辑]