跳到主要内容

std::shared_ptr<T>::operator=

声明

// 1)
shared_ptr& operator=( const shared_ptr& r ) noexcept;

template< class Y >
shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
// 2)
shared_ptr& operator=( shared_ptr&& r ) noexcept;

template< class Y >
shared_ptr& operator=( shared_ptr<Y>&& r ) noexcept;
// 3)
template< class Y >
shared_ptr& operator=( std::auto_ptr<Y>&& r ); // deprecated in C++11, removed in C++17
// 4)
template< class Y, class Deleter >
shared_ptr& operator=( std::unique_ptr<Y,Deleter>&& r );

替换由 r 管理的对象为 r 所管理的对象。

如果 *this 已拥有某个对象,并且它是唯一拥有该对象的 shared_ptr,并且 r 与 *this 不同,则该对象将被通过其拥有的删除器销毁。


共享 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) 可能抛出由实现定义的异常。

示例

本节不完整

std::shared_ptr<T>::operator=

声明

// 1)
shared_ptr& operator=( const shared_ptr& r ) noexcept;

template< class Y >
shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
// 2)
shared_ptr& operator=( shared_ptr&& r ) noexcept;

template< class Y >
shared_ptr& operator=( shared_ptr<Y>&& r ) noexcept;
// 3)
template< class Y >
shared_ptr& operator=( std::auto_ptr<Y>&& r ); // deprecated in C++11, removed in C++17
// 4)
template< class Y, class Deleter >
shared_ptr& operator=( std::unique_ptr<Y,Deleter>&& r );

替换由 r 管理的对象为 r 所管理的对象。

如果 *this 已拥有某个对象,并且它是唯一拥有该对象的 shared_ptr,并且 r 与 *this 不同,则该对象将被通过其拥有的删除器销毁。


共享 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) 可能抛出由实现定义的异常。

示例

本节不完整