◐ Shell
clean mode source ↗

std::shared_ptr<T>::operator= - cppreference.com

提供: cppreference.com

<tbody> </tbody>

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) shared_ptrr からムーブ代入します。 代入後、 *thisr の以前の状態のコピーを格納し、 r は空になります。 shared_ptr<T>(std::move(r)).swap(*this) と同等です。

3) r によって管理されているオブジェクトの所有権を *this に転送します。 r がオブジェクトを管理していない場合は *this もオブジェクトを管理しません。 代入後、 *thisr がそれまで保持していたポインタを格納し、 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) 例外を投げるかもしれません。

関連項目