名稱空間
變體
操作

std::shared_ptr<T>::operator=

來自 cppreference.com
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
 
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) [edit]