Namespaces
Variants

std::shared_ptr<T>:: operator=

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until 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) 可能抛出由实现定义的异常。

示例

参见

替换被管理对象
(公开成员函数)