std::shared_ptr<T>:: operator=
From cppreference.net
<
cpp
|
memory
|
shared ptr
C++
Memory management library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::shared_ptr
| Member functions | ||||
|
shared_ptr::operator=
|
||||
| Modifiers | ||||
| Observers | ||||
|
(C++17)
|
||||
|
(
until C++20*
)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Non-member functions | ||||
|
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(C++20)
|
||||
|
functions
(
until C++26*
)
|
||||
| Helper classes | ||||
|
(C++20)
|
||||
| Deduction guides (C++17) |
|
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)
可能抛出由实现定义的异常。
示例
|
本节内容不完整
原因:缺少示例 |
参见
|
替换被管理对象
(公开成员函数) |