當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Unique_ptr用法及代碼示例

std::unique_ptr 是 C++11 中引入的智能指針。它自動管理堆上動態分配的資源。智能指針隻是常規舊指針的包裝,可以幫助您防止廣泛的錯誤。即忘記刪除指針而導致內存泄漏,或者意外刪除指針兩次或以錯誤的方式刪除指針。它們的使用方式與標準指針類似。他們將一些導致常見錯誤的手動流程自動化。

先決條件: C++中的指針,C++ 中的智能指針。

用法

unique_ptr<A> ptr1 (new A)

這裏,

  • unique_ptr<A>:它指定 std::unique_ptr 的類型。在本例中 - 類型 A 的對象。
  • 新A:使用 new 運算符在堆上動態分配 A 類型的對象。
  • 指針1:這是 std::unique_ptr 變量的名稱。

使用unique_ptr 時會發生什麽?

當我們寫入 unique_ptr<A> ptr1 (new A) 時,會在堆上為數據類型 A 的實例分配內存。 ptr1 被初始化並指向新創建的 A 對象。在這裏,ptr1 是新創建的對象 A 的唯一所有者,它管理該對象的生命周期。這意味著當 ptr1 被重置或超出範圍時,內存將自動釋放並且 A 的對象被銷毀。

何時使用unique_ptr?

當需要資源所有權時。當我們想要資源的單一或獨占所有權時,我們應該選擇唯一的指針。隻有一個唯一的指針可以指向一種資源。因此,一個唯一的指針不能複製到另一個。此外,當動態分配的對象超出範圍時,它有助於自動清理,並有助於防止內存泄漏。

Note: We need to use the <memory> header file for using these smart pointers.

Unique_ptr 的示例

示例 1:

讓我們創建一個結構體 A,它將有一個名為 printA 的方法來顯示一些文本。然後在主部分中,讓我們創建一個指向結構體 A 的唯一指針。因此,此時,我們有一個結構體 A 的實例,並且 p1 保存指向該實例的指針。

C++


// C++ Program to implement unique_ptr 
#include <iostream> 
#include <memory> 
using namespace std; 
  
struct A { 
    void printA() { cout << "A struct...." << endl; } 
}; 
  
int main() 
{ 
    unique_ptr<A> p1(new A); 
    p1->printA(); 
  
    // displays address of the containing pointer 
    cout << p1.get() << endl; 
    return 0; 
}
輸出
A struct....
0x18dac20



示例 2

現在讓我們創建另一個指針 p2,並嘗試使用賦值運算符 (=) 複製指針 p1。

C++


// C++ Program to implement unique_ptr 
#include <iostream> 
#include <memory> 
using namespace std; 
  
struct A { 
    void printA() { cout << "A struct...." << endl; } 
}; 
int main() 
{ 
    unique_ptr<A> p1(new A); 
    p1->printA(); 
  
    // displays address of the containing pointer 
    cout << p1.get() << endl; 
  
    // will give compile time error 
    unique_ptr<A> p2 = p1; 
    p2->printA(); 
    return 0; 
}

輸出

main.cpp: In function ‘int main()’:
main.cpp:18:24: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete]’
18 | unique_ptr<A> p2 = p1;
| ^~
In file included from /usr/include/c++/11/memory:76,
from main.cpp:3:
/usr/include/c++/11/bits/unique_ptr.h:468:7: note: declared here
468 | unique_ptr(const unique_ptr&) = delete;
| ^~~~~~~~~~

上麵的代碼將給出編譯時錯誤,因為在唯一指針的情況下我們無法將指針 p2 分配給 p1。我們必須使用移動語義來達到這樣的目的,如下所示。

實施例3

使用移動語義管理類型 A 的對象。

C++


// C++ Program to implement unique_ptr 
#include <iostream> 
#include <memory> 
using namespace std; 
  
struct A { 
    void printA() { cout << "A struct...." << endl; } 
}; 
int main() 
{ 
    unique_ptr<A> p1(new A); 
    p1->printA(); 
    
    // displays address of the containing pointer 
    cout << p1.get() << endl; 
  
    // now address stored in p1 shpould get copied to p2 
    unique_ptr<A> p2 = move(p1); 
  
    p2->printA(); 
    cout << p1.get() << endl; 
    cout << p2.get() << endl; 
    return 0; 
}
輸出
A struct....
0x2018c20
A struct....
0
0x2018c20



注意,一旦指針 p1 中的地址複製到指針 p2 中,指針 p1 的地址將變為 NULL(0),並且 p2 存儲的地址現在與 p1 存儲的地址相同,表明 p1 中的地址已轉移到指針p2 使用移動語義。



相關用法


注:本文由純淨天空篩選整理自pritamauddy25大神的英文原創作品 Unique_ptr in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。