std::shared_ptr<T>::reset
來自 cppreference.com
< cpp | 記憶體 | shared ptr
void reset() noexcept; |
(1) | (C++11 起) |
template< class Y > void reset( Y* ptr ); |
(2) | (C++11 起) |
template< class Y, class Deleter > void reset( Y* ptr, Deleter d ); |
(3) | (C++11 起) |
template< class Y, class Deleter, class Alloc > void reset( Y* ptr, Deleter d, Alloc alloc ); |
(4) | (C++11 起) |
將託管物件替換為 ptr 指向的物件。可以提供可選的刪除器 d,當沒有 shared_ptr
物件擁有新物件時,此刪除器將用於銷燬新物件。預設情況下,delete 表示式用作刪除器。始終選擇與提供的型別對應的適當的 delete 表示式,這就是該函式作為使用單獨引數 Y
的模板實現的原因。
如果 *this 已經擁有一個物件,並且它是最後一個擁有該物件的 shared_ptr
,則透過所擁有的刪除器銷燬該物件。
如果 ptr 指向的物件已被擁有,則該函式通常會導致未定義行為。
1) 釋放託管物件(如果有)的所有權。呼叫後,*this 不管理任何物件。等效於 shared_ptr().swap(*this);。
2-4) 將託管物件替換為 ptr 指向的物件。
Y
必須是完整型別且可隱式轉換為 T
。此外2) 使用 delete 表示式作為刪除器。必須提供有效的 delete 表示式,即 delete ptr 必須格式良好,具有明確定義的行為且不丟擲任何異常。等效於 shared_ptr<T>(ptr).swap(*this);。
3) 使用指定的刪除器 d 作為刪除器。
Deleter
必須可用於型別 T
,即 d(ptr) 必須格式良好,具有明確定義的行為且不丟擲任何異常。Deleter
必須是 可複製構造的(CopyConstructible),其複製建構函式和解構函式不得丟擲異常。等效於 shared_ptr<T>(ptr, d).swap(*this);。4) 與 (3) 相同,但另外使用 alloc 的副本為內部使用的資料分配記憶體。
Alloc
必須是 分配器(Allocator)。複製建構函式和解構函式不得丟擲異常。等效於 shared_ptr<T>(ptr, d, alloc).swap(*this);。目錄 |
[編輯] 引數
ptr | - | 指向要獲取所有權的物件指標 |
d | - | 用於刪除物件的刪除器 |
alloc | - | 用於內部分配的分配器 |
[編輯] 返回值
(無)
[編輯] 異常
[編輯] 示例
執行此程式碼
#include <iostream> #include <memory> struct Foo { Foo(int n = 0) noexcept : bar(n) { std::cout << "Foo::Foo(), bar = " << bar << " @ " << this << '\n'; } ~Foo() { std::cout << "Foo::~Foo(), bar = " << bar << " @ " << this << '\n'; } int getBar() const noexcept { return bar; } private: int bar; }; int main() { std::cout << "1) unique ownership\n"; { std::shared_ptr<Foo> sptr = std::make_shared<Foo>(100); std::cout << "Foo::bar = " << sptr->getBar() << ", use_count() = " << sptr.use_count() << '\n'; // Reset the shared_ptr without handing it a fresh instance of Foo. // The old instance will be destroyed after this call. std::cout << "call sptr.reset()...\n"; sptr.reset(); // calls Foo's destructor here std::cout << "After reset(): use_count() = " << sptr.use_count() << ", sptr = " << sptr << '\n'; } // No call to Foo's destructor, it was done earlier in reset(). std::cout << "\n2) unique ownership\n"; { std::shared_ptr<Foo> sptr = std::make_shared<Foo>(200); std::cout << "Foo::bar = " << sptr->getBar() << ", use_count() = " << sptr.use_count() << '\n'; // Reset the shared_ptr, hand it a fresh instance of Foo. // The old instance will be destroyed after this call. std::cout << "call sptr.reset()...\n"; sptr.reset(new Foo{222}); std::cout << "After reset(): use_count() = " << sptr.use_count() << ", sptr = " << sptr << "\nLeaving the scope...\n"; } // Calls Foo's destructor. std::cout << "\n3) multiple ownership\n"; { std::shared_ptr<Foo> sptr1 = std::make_shared<Foo>(300); std::shared_ptr<Foo> sptr2 = sptr1; std::shared_ptr<Foo> sptr3 = sptr2; std::cout << "Foo::bar = " << sptr1->getBar() << ", use_count() = " << sptr1.use_count() << '\n'; // Reset the shared_ptr sptr1, hand it a fresh instance of Foo. // The old instance will stay shared between sptr2 and sptr3. std::cout << "call sptr1.reset()...\n"; sptr1.reset(new Foo{333}); std::cout << "After reset():\n" << "sptr1.use_count() = " << sptr1.use_count() << ", sptr1 @ " << sptr1 << '\n' << "sptr2.use_count() = " << sptr2.use_count() << ", sptr2 @ " << sptr2 << '\n' << "sptr3.use_count() = " << sptr3.use_count() << ", sptr3 @ " << sptr3 << '\n' << "Leaving the scope...\n"; } // Calls two destructors of: 1) Foo owned by sptr1, // 2) Foo shared between sptr2/sptr3. }
可能的輸出
1) unique ownership Foo::Foo(), bar = 100 @ 0x23c5040 Foo::bar = 100, use_count() = 1 call sptr.reset()... Foo::~Foo(), bar = 100 @ 0x23c5040 After reset(): use_count() = 0, sptr = 0 2) unique ownership Foo::Foo(), bar = 200 @ 0x23c5040 Foo::bar = 200, use_count() = 1 call sptr.reset()... Foo::Foo(), bar = 222 @ 0x23c5050 Foo::~Foo(), bar = 200 @ 0x23c5040 After reset(): use_count() = 1, sptr = 0x23c5050 Leaving the scope... Foo::~Foo(), bar = 222 @ 0x23c5050 3) multiple ownership Foo::Foo(), bar = 300 @ 0x23c5080 Foo::bar = 300, use_count() = 3 call sptr1.reset()... Foo::Foo(), bar = 333 @ 0x23c5050 After reset(): sptr1.use_count() = 1, sptr1 @ 0x23c5050 sptr2.use_count() = 2, sptr2 @ 0x23c5080 sptr3.use_count() = 2, sptr3 @ 0x23c5080 Leaving the scope... Foo::~Foo(), bar = 300 @ 0x23c5080 Foo::~Foo(), bar = 333 @ 0x23c5050
[編輯] 參閱
構造新的 shared_ptr (public member function) |