日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ share_prt 简单设计和实现

發布時間:2023/12/13 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ share_prt 简单设计和实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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; }

?

轉載于:https://www.cnblogs.com/cycxtz/p/4742970.html

總結

以上是生活随笔為你收集整理的C++ share_prt 简单设计和实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。