C++智能指針shared_ptr分析

C++智能指針shared_ptr分析

創(chuàng)新互聯(lián)主營(yíng)甘州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),甘州h5小程序開(kāi)發(fā)搭建,甘州網(wǎng)站營(yíng)銷(xiāo)推廣歡迎甘州等地區(qū)企業(yè)咨詢(xún)

概要:

shared_ptr是c++智能指針中適用場(chǎng)景多,功能實(shí)現(xiàn)較多的智能指針。它采取引用計(jì)數(shù)的方法來(lái)實(shí)現(xiàn)釋放指針?biāo)赶虻馁Y源。下面是我代碼實(shí)現(xiàn)的基本功能。

實(shí)例代碼:

template<class T>
class sharedptr
{
public:
  sharedptr(T* ptr)
  :_ptr(ptr)
  , _refCount(new int(1))
  {}

  sharedptr(sharedptr<T>& sp)
    :_ptr(sp._ptr)
    , _refCount(sp._refCount)
  {
    ++(*_refCount);
  }
  sharedptr& operator = (sharedptr<T>& sp) //現(xiàn)代寫(xiě)法
  {
    swap(_ptr, sp._ptr);
    swap(_refCount, sp._refCount);
    return *this;
  }

  /*sharedptr& operator = (sharedptr<T>& sp)
  {
    if (this != &sp)
    {
      this->Release();

      _ptr = sp._ptr;
      _refCount = sp._refCount;
      ++(*_refCount);
    }
    return *this;
  }*/
  void Release()
  {
    if (--(*_refCount) == 0)
    {
      delete _ptr;
      delete _refCount;
    }
  }
  ~sharedptr()
  {
    Release();
  }
private:
  T* _ptr;
  int* _refCount;
};

但是呢這只能刪除基本類(lèi)型,例int、double類(lèi)型指針。但對(duì)于數(shù)組指針不是適用。在c++中動(dòng)態(tài)內(nèi)存管理中,new/delete對(duì)應(yīng),new[]/delete[]對(duì)應(yīng),所以就引入了定制刪除器這個(gè)概念。

定制刪除器基本的實(shí)現(xiàn)就是:

template<class T>
struct Delete
{
  void operator()(T* ptr)
  {
    delete ptr;
  }
};

template<class T>
struct DeleteArray
{
  void operator()(T* ptr)
  {
    delete[] ptr;
  }
};

如何在shared_ptr中使用呢,下來(lái)我用代碼簡(jiǎn)單做個(gè)示范。

template<class T> //定制刪除器
struct Delete
{
  void operator()(T* ptr)
  {
    delete ptr;
  }
};

template<class T>//定制刪除器
struct DeleteArray
{
  void operator()(T* ptr)
  {
    delete[] ptr;
  }
};

template<class T, class D = Delete<T>>
class sharedptr
{
public:
  sharedptr(T* ptr, D del)
    :_ptr(ptr)
    , _refCount(new int(1))
    , _del(del)
  {}

  sharedptr(sharedptr<T>& sp)
    :_ptr(sp._ptr)
    , _refCount(sp._refCount)
  {
    ++(*_refCount);
  }
  sharedptr& operator = (sharedptr<T>& sp) //現(xiàn)代寫(xiě)法
  {
    swap(_ptr, sp._ptr);
    swap(_refCount, sp._refCount);
    return *this;
  }

  /*sharedptr& operator = (sharedptr<T>& sp)
  {
    if (this != &sp)
    {
      this->Release();

      _ptr = sp._ptr;
      _refCount = sp._refCount;
      ++(*_refCount);
    }
    return *this;
  }*/
  void Release()
  {
    if (--(*_refCount) == 0)
    {
      printf("delete:0x%p\n", _ptr);
      _del(_ptr);
      delete _refCount;
    }
  }
  ~sharedptr()
  {
    Release();
  }
private:
  T* _ptr;
  int* _refCount;
  D _del;
};

在調(diào)用時(shí),應(yīng)這樣寫(xiě)調(diào)用函數(shù):

void TextSharedptr()
{
  DeleteArray<int> da;
  sharedptr<int, DeleteArray<int>> sp(new int[3], da);

}

而weak_ptr弱引用智能指針是通過(guò)(引用不增加計(jì)數(shù))來(lái)打破循環(huán)引用的;

什么循環(huán)引用,這可以簡(jiǎn)單用一個(gè)雙向鏈表來(lái)解釋?zhuān)?

C++智能指針shared_ptr分析

而weak_ptr通過(guò)只引用不增加計(jì)數(shù)的方法打破了循環(huán)引用這個(gè)問(wèn)題。但在使用weak_ptr的前提是確定在使用shared_ptr智能指針時(shí)存在循環(huán)引用這個(gè)問(wèn)題。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

標(biāo)題名稱(chēng):C++智能指針shared_ptr分析
分享地址:http://www.muchs.cn/article18/ishsgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、企業(yè)建站、網(wǎng)站策劃、網(wǎng)站改版、響應(yīng)式網(wǎng)站品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司