C++ 智能指標auto_ptr類

來源:互聯網
上載者:User

 

轉載自:http://www.cnblogs.com/mydomain/archive/2011/04/15/2017424.html

 

1、auto_ptr為標準庫提供的“資源分派即初始化”類,是接受一個類型形參的模板,它為動態分配的對象提供異常安全特性。在memory標頭檔中定義。

2、auto_ptr操作

auto_ptr<T> ap;

建立名為 ap 的未綁定的 auto_ptr 對象

auto_ptr<T> ap(p);

建立名為 ap 的 auto_ptr 對象,ap 擁有指標 p 指向的對象。該建構函式為 explicit

auto_ptr<T> ap1(ap2);

建立名為 ap1 的 auto_ptr 對象,ap1 儲存原來儲存在ap2 中的指標。將所有權轉給 ap1,ap2 成為未綁定的auto_ptr 對象

ap1 = ap2

將所有權 ap2 轉給 ap1。刪除 ap1 指向的對象並且使 ap1指向 ap2 指向的對象,使 ap2 成為未綁定的

~ap

解構函式。刪除 ap 指向的對象

*ap

返回對 ap 所綁定的對象的引用

ap->

返回 ap 儲存的指標

ap.reset(p)

如果 p 與 ap 的值不同,則刪除 ap 指向的對象並且將 ap綁定到 p

ap.release()

返回 ap 所儲存的指標並且使 ap 成為未綁定的

ap.get()

返回 ap 儲存的指標

    auto_ptr只能用於管理從new返回的一個對象,它不能管理動態分配的數組。當auto_ptr被複製或賦值的時候,有不尋找的行為,因此不能將auto_ptr儲存在標準庫容器類中。

3、每個auto_ptr對象綁定到一個對象或者指向一個對象。當auto_ptr對象指向一個對象的時候,可以說它“擁有”該對象。當auto_ptr對象超出範圍或者另外撤銷的時候,就自動回收auto_ptr所指向的動態指派至。

    auto_ptr是可以儲存任何類型指標的模板。

樣本

view sourceprint?
void f()  {      auto_ptr<int> ap(newint(42)); // allocate a new object      // code that throws an exception that is not caught inside f }   // auto_ptr freed automatically when function ends 

4、注意到,接受指標的建構函式為explicit建構函式,所以必須用初始化的直接形式來建立auto_ptr對象。

樣本

view sourceprint?
// error: constructor that takes a pointer is explicit and can't be used implicitly auto_ptr<int> pi = new int(1024);  auto_ptr<int> pi(new int(1024)); // ok: uses direct initialization 

pi所指的由new運算式建立的對象在超出範圍時自動刪除。

5、auto_ptr的主要目的是在保證自動刪除auto_ptr對象引用的對象的同時,支援普通指標式行為。

樣本

view sourceprint?
auto_ptr<string> ap1(new string("Hellobaby!")); *ap1 = "TRex"; // assigns a new value to the object to which ap1 points string s = *ap1; // initializes s as a copy of the object to which ap1 points if (ap1->empty()) // runs empty on the string to which ap1 points 

6、auto_ptr對象的賦值和複製是破壞性操作行為,與普通指標的複製和賦值有區別。普通指標賦值或複製後兩個指標指向同一對象,而auto_ptr對象複製或賦值後,將基礎對象的所有權從原來的auto_ptr對象轉給副本,原來的auto_ptr對象重設成為未綁定狀態。

    因此,auto_ptr賦值的左右運算元必須是可修改的左值。因為標準庫容器要求在複製或賦值後兩對象相等,所以auto_ptr不能儲存在標準容器中。

7、預設情況下,auto_ptr的內部指標值置為0。

8、測試auto_ptr對象

    auto_ptr 類型沒有定義到可用作條件的類型的轉換,相反,要測試auto_ptr 對象,必須使用它的 get 成員,該成員返回包含在 auto_ptr 對象中的基礎指標。

樣本

view sourceprint?
// error: cannot use an auto_ptr as a condition  if (p_auto)     *p_auto = 1024;  // revised test to guarantee p_auto refers to an object  if (p_auto.get())     *p_auto = 1024; 

    應該只用 get 詢問 auto_ptr 對象或者使用返回的指標值,不能用 get 作為建立其他 auto_ptr 對象的實參。

9、reset操作

不能直接將一個地址(或其它指標)賦給auto_ptr對象。

樣本

view sourceprint?
#include <iostream>  #include "memory"  using namespace std;     int main()  {      auto_ptr<int> p_auto(newint(123));      //p_auto = new int(1024); // error: cannot assign a pointer to an auto_ptr     if (p_auto.get())          *p_auto = 1024;      else         p_auto.reset(new int(1042));     return 1;  } 

    正如自身賦值是沒有效果的一樣,如果調用該 auto_ptr 對象已經儲存的同一指標的 reset 函數,也沒有效果,不會刪除對象。

10、正確使用auto_ptr類,有如下限制:

1)不要使用auto_ptr對象儲存指向靜態指派至的指標,否則,當auto_ptr對象本身被撤銷的時候,它將試圖刪除指向非動態指派至的指標,導致未定義的行為。

2)永遠不要使用兩個 auto_ptrs 對象指向同一對象,導致這個錯誤的一種明顯方式是,使用同一指標來初始化或者 reset 兩個不同的 auto_ptr 對象。另一種導致這個錯誤的微妙方式可能是,使用一個 auto_ptr 對象的 get 函數的結果來初始化或者 reset另一個 auto_ptr 對象。

3)不要使用 auto_ptr 對象儲存指向動態分配數組的指標。當auto_ptr 對象被刪除的時候,它只釋放一個對象—它使用普通delete 操作符,而不用數組的 delete [] 操作符。

4)不要將 auto_ptr Object Storage Service在容器中。容器要求所儲存的類型定義複製和賦值操作符,使它們表現得類似於內建類型的操作符:在複製(或者賦值)之後,兩個對象必須具有相同值,auto_ptr 類不滿足這個要求。

11、異常說明

1)定義

樣本

view sourceprint?
void recoup(int) throw(runtime_error); void no_problem() throw(); //空說明列表指出函數不拋出任何異常 

異常說明是函數介面的一部分,函數定義以及該函數的任意聲明必須具有相同的異常說明。如果函式宣告沒有指定異常說明,則該函數可以拋出任意類型的異常。

2)基類的虛函數的異常說明可以與衍生類別中對應虛函數的異常說明不同,但衍生類別虛函數的異常說明應當更嚴格。基類中虛函數異常列表是衍生類別異常列表的超集。

12、可以在函數指標的定義中提供異常說明。在用另一指標初始化帶異常說明的函數的指標,或者將後者賦值給函數地址的時候,兩個指標的異常說明不必相同,但是,源指標的異常說明必須至少與目標指標的一樣嚴格。

樣本

view sourceprint?
void recoup(int) throw(runtime_error); // ok: recoup is as restrictive as pf1  void (*pf1)(int) throw(runtime_error) = recoup; // ok: recoup is more restrictive than pf2  void (*pf2)(int) throw(runtime_error, logic_error) = recoup; // error: recoup is less restrictive than pf3  void (*pf3)(int) throw() = recoup; // ok: recoup is more restrictive than pf4  void (*pf4)(int) = recoup; 

參考

[1]

http://www.cnblogs.com/mydomain/archive/2011/04/14/2016565.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.