std::shared_ptr<T>::operator=
來自 cppreference.com
< cpp | 記憶體 | shared ptr
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)。
目錄 |
[edit] 引數
r | - | 另一個智慧指標,用於共享所有權或從中獲取所有權 |
[edit] 返回值
*this
[edit] 注意
實現可能無需建立臨時 shared_ptr
物件即可滿足要求。
[edit] 異常
5,6) 可能會丟擲實現定義的異常。
[edit] 示例
本節不完整 原因:無示例 |
[edit] 另請參閱
替換託管物件 (public member function) |