std::shared_ptr<T>::operator=
来自cppreference.com
< cpp | memory | shared ptr
工具库
动态内存管理
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::shared_ptr
| 成员函数 | ||||
|
shared_ptr::operator=
|
||||
| 修改器 | ||||
| 观察器 | ||||
|
(C++17)
|
||||
|
(C++20
前)
|
||||
| 非成员函数 | ||||
|
(C++20
前)(C++20
前)(C++20
前)(C++20
前)(C++20
前)(C++20)
|
||||
|
函数(C++20
中弃用)
|
||||
| 辅助类 | ||||
|
(C++20)
|
||||
| 推导指引(C++17) |
|
shared_ptr&
operator=(
const
shared_ptr&
r )
noexcept;
|
(1) | |
|
template<
class Y
>
shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept; |
(1) | |
|
shared_ptr&
operator=(
shared_ptr&&
r )
noexcept;
|
(2) | |
|
template<
class Y
>
shared_ptr& operator=( shared_ptr<Y>&& r ) noexcept; |
(2) | |
|
template<
class Y
>
shared_ptr& operator=( std::auto_ptr<Y>&& r ); |
(3) | (C++11 中弃用) (C++17 中移除) |
|
template<
class
Y, class
Deleter >
shared_ptr& operator=( std::unique_ptr<Y,Deleter>&& r ); |
(4) | |
以 r 所管理者替换被管理对象。
若 *this 已占有对象且它是最后一个占有该对象的 shared_ptr ,且
r 与 *this 不相同,则通过占有的删除器销毁对象。
1) 共享
r 所管理对象的所有权。若
r 不管理对象,则 *this 亦不管理对象。等价于 shared_ptr<T>(r).swap(*this) 。2) 从
r 移动赋值
shared_ptr 。赋值后, *this 含有先前
r 状态的副本,而 r 为空,等价于 shared_ptr<T>(std::move(r)).swap(*this) 。3) 转移
r 所管理对象的所有权给
*this 。若 r 不管理对象,则 *this
亦不管理对象。赋值后, *this 含有先前 r 所保有的对象,且 use_count()==1 ;而
r 为空。等价于 shared_ptr<T>(r).swap(*this) 。4) 转移
r 所管理对象的所有权给
*this 。为将来删除被管理对象,存储关联到 r 的删除器。调用后
r 不管理对象。等价于 shared_ptr<T>(std::move(r)).swap(*this) 。参数
| r | - | 要获得所有权或共享所有权的另一智能指针 |
返回值
*this
注解
实现可以满足要求而无需创建临时的 shared_ptr 对象。
异常
3,4) 可能会抛出由实现定义的异常。
示例
| 本节未完成 原因:暂无示例 |
参阅
| 替换所管理的对象 (公开成员函数) |