C++ share_prt 簡單設計和實現

來源:互聯網
上載者:User

標籤:

C++ 比較煩惱的是記憶體的管理,new是簡單,不夠,delete偶爾就會忘記。或者說,出現,多個對象共用多一個記憶體,一個delete以後,導致其他也不能用的不良情況,所以就跑出了一個智能指標來進行管理。

 

設計需求:

1.該智能指標能接受各種類型的指標 -- 使用模板

2.智能指標需要知道該對象有多少個人在用他 -- 大家用的同一個計數器 int * num;

3.共同使用同一個記憶體 --  資料指標 T * data;

ps:目前為線程不安全的實現,若要達到安全執行緒,需要使用一個共同的互斥變數。

 

#ifndef SHARE_PTR_H#define SHARE_PTR_Htemplate<typename T>class share_ptr{private:    T * data;    int * num;public:    /**      * @brief 建構函式,通過傳入的對象指標進行初始化,      *     並將計數器置1      */    share_ptr(T * t):data(t);    /**      * @brief 解構函式,判斷當前對象是否為最後一個,      *     是就delete,不是就計數減1      */    ~share_ptr();        /**      * @brief 拷貝建構函式,通過rhs的值賦值,      *     並將計數器加1      */    share_ptr(share_ptr<T>& rhs);    /**      * @brief 賦值,判斷當前對象是否一致,      *     是則返回,不是則析構之前的,並用現在的賦值,      *        計數器加1      */    share_ptr<T>& operator =( share_ptr<T>& rhs);        /**      * @brief 返回資料的引用      */    T& operator *(){ return *data; }          /**      * @brief 返回資料的指標      */    T* operator ->() { return data; }          /**      * @brief 擷取當前有多少個共同使用者      */    int count(){ return *num;}};#endif //SHARE_PTR_H

 

實現

#ifndef SHARE_PTR_H#define SHARE_PTR_H#include <stdio.h>#include <iostream>#ifdef _DEBUG#define DEBUG(fmt)  printf (fmt) #else#define DEBUG(x)  #endiftemplate<typename T>class share_ptr{private:    T * data;    int * num;public:    share_ptr(T * t):data(t){        num = new int;        *num = 1;        DEBUG("share_ptr(T * t):data(t)\n");    }    ~share_ptr(){        if(*num>1){            (*num)--;            DEBUG("~share_ptr,num = k\n");        }else{            DEBUG("~share_ptr,num = 0\n");            delete data;            delete num;            data = NULL;            num = NULL;        }    }    //拷貝構造    share_ptr(share_ptr<T>& rhs){        data = rhs.data;        num = rhs.num;        (*num)++;        DEBUG("share_ptr(share_ptr<T>& rhs)\n");    }    //賦值    share_ptr<T>& operator =( share_ptr<T>& rhs){        if( data == rhs.data){            DEBUG("share_prt<T>& operator=(share_ptr<T>& rhs)\n");            return *this;        }else{            //判斷本來指向的指標是否是最後一個,是的話,就delete掉,不是的話就*num--            if(*num == 1){                delete data;                delete num;                data = NULL;                num = NULL;            }else{ (*num)--; }            data = rhs.data;            num = rhs.num;            (*num)++;        }        DEBUG("share_prt<T>& operator=(share_ptr<T>& rhs)\n");        return *this;    }    T& operator *(){ return *data; }      T* operator ->() { return data; }      int count(){ return *num;}};#endif //SHARE_PTR_H

 

測試:

#include <iostream>#include <string>#include "share_ptr.h"using namespace std;int main(){    {        share_ptr<string> ps( new string("hello world") );        cout << *ps <<" count:"<<  ps.count() << endl;        share_ptr<string> ps2(ps);        cout << *ps2 <<" count:"<<  ps2.count() << endl;        share_ptr<string> ps3 = ps;        cout << *ps3 <<" count:"<<  ps2.count() << endl;        ps2 = ps3;        cout << *ps2 <<" count:"<<  ps2.count() << endl;    }    system("pause");    return 0;}

 

C++ share_prt 簡單設計和實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.