【C++深度剖析學習總結】 37 智能指針分析

【C++深度剖析學習總結】 37 智能指針分析

作者 CodeAllen ,轉載請註明出處


1.永恆的話題
內存泄漏(臭名昭著的Bug)

  • 動態申請堆空間,用完後不歸還,完全依賴程序員本身
  • C++語言中沒有垃圾回收的機制
  • 指針無法控制所指堆空間的生命週期

大公司一般會要求嚴格的代碼規範

實驗1 內存泄漏

#include <iostream>
#include <string>
using namespace std;
class Test
{
    int i;
public:
    Test(int i)
    {
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
    }
};
int main()
{
    for(int i=0; i<5; i++)
    {
        Test* p = new Test(i);//此處造成內存泄漏
       
        cout << p->value() << endl;
       
    }
   
    return 0;
}

2.深度的思考
我們需要什麼

  • 需要一個特殊的指針
  • 指針生命週期結束時主動釋放堆空間
  • 一片堆空間最多隻能由一個指針標識
  • 杜絕指針運算和指針比較

3.智能指針分析
解決方案

  • 重載指針特徵操作符(-> 和 *)
  • 只能通過類的成員函數重載
  • 重載函數不能使用參數
  • 只能定義一個重載函數(3,4條說的規定含義相同)

實驗2 智能指針 (這個類就是智能指針的創建過程)

#include <iostream>
#include <string>
using namespace std;
class Test
{
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};
class Pointer
{
    Test* mp;
public:
    Pointer(Test* p = NULL)
    {
                  mp = p;
    }
    Pointer(const Pointer& obj)
    {
                  //delete mp; //此處存在則存在野指針,報錯
                  mp = obj.mp;
                  const_cast<Pointer&>(obj).mp = NULL;
    }
    Pointer& operator = (const Pointer& obj)
    {
         if(this != &obj )
         {
             delete mp;
             mp = obj.mp;
                  const_cast<Pointer&>(obj).mp = NULL;
                  }
                  return *this;
    }
    Test* operator -> ()
    {
                  return mp;
    }
    Test& operator * ()
    {
                  return *mp;
    }
    bool isNull()
    {
                  return (mp == NULL);
    }
    ~Pointer()
    {
                  delete mp;
    }
};
int main()
{
    Pointer p1 = new Test(0);
   
    cout << p1->value() << endl;
   
    Pointer p2 = p1;
   
    cout << p1.isNull() << endl;
   
    cout << p2->value() << endl;
   
    return 0;
}

運行結果
Test(int i)
0
1
0
~Test()

智能指針的使用軍規:只能用來指向堆空間中的對象或者變量

小結
指針特徵操作符(-> 和 *)
重載指針特徵符能夠使用對象代替指針
智能指針只能用於指向堆空間中的內存
智能指針的意義在於最大程度的避免內存問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章