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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++智能指针(二)模拟实现三种智能指针

發(fā)布時(shí)間:2023/11/30 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++智能指针(二)模拟实现三种智能指针 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

https://blog.csdn.net/nou_camp/article/details/70186721

在上一篇博客中提到了Auto_ptr(C++智能指針(一)),下面進(jìn)行模擬實(shí)現(xiàn)Auto_ptr?
采用類模板實(shí)現(xiàn)

#include<iostream> using namespace std; template<class T> class Autoptr { public:Autoptr(T* ptr = NULL):_ptr(ptr){}//Autoptr():_ptr(NULL)//{}Autoptr(Autoptr<T>& ap){this->_ptr = ap._ptr;ap._ptr = NULL;}Autoptr<T>& operator=(Autoptr<T>& sp){if (this != &sp){delete this->_ptr;_ptr = sp._ptr;sp._ptr = NULL;}return *this;}T* operator->(){return _ptr;}T operator*(){return *_ptr;}~Autoptr(){delete _ptr;_ptr = NULL;}void Reset(T* ptr = 0){if (_ptr != ptr){delete _ptr;}_ptr = ptr;} protected:T* _ptr; };void test() {//int *p1 = new int(10);//Autoptr<int>ap1(p1);Autoptr<int> ap1(new int(10));//上面的兩行代碼可以直接用本行代碼代替cout << *ap1 << endl;Autoptr<int> ap2(ap1);cout << *ap2 << endl;Autoptr<int> ap3(new int(20));ap3 = ap2;cout << *ap3 << endl;//cout << *ap1 << endl;//會(huì)使代碼出錯(cuò) } int main() {test();system("pause");return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

?


//cout << *ap1 << endl;//會(huì)使代碼出錯(cuò)?
這是test函數(shù)中的代碼,執(zhí)行這句代碼,會(huì)使程序崩潰,這是因?yàn)閍p1已經(jīng)把它指向的空間交給了ap2去管理,所以ap1已經(jīng)不具備訪問(wèn)原來(lái)自己所指向的空間的權(quán)限。所以對(duì)它進(jìn)行解引用是非法的。?
所以可以看清auto_ptr的本質(zhì)是管理權(quán)的轉(zhuǎn)移,即ap1將自己所指向的空間交給ap2來(lái)管理,析構(gòu)也是由ap2來(lái)完成。


由上面可知auto_ptr有嚴(yán)重缺陷,所以后來(lái)有人寫(xiě)了scopedptr,慢慢發(fā)展形成了第三方庫(kù)。?
scopedptr ->防拷貝,意思就是不能進(jìn)行拷貝,簡(jiǎn)單地說(shuō)是一種簡(jiǎn)單粗暴的方式。下面模擬實(shí)現(xiàn)scopedptr。采用模板類實(shí)現(xiàn)。

template<class T> class scopedptr { public:scopedptr(T *ptr):_ptr(ptr){}T* operator->(){return _ptr;}T operator*(){return *_ptr;}~scopedptr(){delete _ptr;_ptr = NULL;} protected:scopedptr<T>& operator=(const scopedptr<T>& s);scopedptr(scopedptr<T>& ap); private:T* _ptr; }; void test() {scopedptr<int> s1(new int(10));//scopedptr<int> s2(s1);//有錯(cuò)誤,編譯不通過(guò)cout << *s1 << endl; } int main() {test();system("pause");return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

scopedptr中對(duì)拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的重載函數(shù)只是進(jìn)行了聲明,并沒(méi)有去定義這兩個(gè)函數(shù),而且聲明為protected或者是private,這是防止別人在類外對(duì)這兩個(gè)函數(shù)進(jìn)行定義。防止拷貝,所以說(shuō)scopedptr是一種簡(jiǎn)單粗暴的方式。


編寫(xiě)程序往往要用到拷貝,這樣scopedptr就不能起到相應(yīng)的作用,所以便有了shared_ptr。?
shared_ptr->采用了引用計(jì)數(shù),優(yōu)點(diǎn)是功能強(qiáng)大,但是也有缺點(diǎn),缺點(diǎn)是過(guò)于復(fù)雜,而且會(huì)引起循環(huán)引用。?
下面模擬實(shí)現(xiàn)shared_ptr

#include<iostream> using namespace std; template<class T> class sharedptr { public:sharedptr(T* ptr) //構(gòu)造:_ptr(ptr), _refcount(new int(1)){}sharedptr() //構(gòu)造:_ptr(NULL), _refcount(new int(1)){}sharedptr(const sharedptr<T>& sp) //拷貝構(gòu)造:_ptr(sp._ptr), _refcount(sp._refcount){(*_refcount)++;}sharedptr<T>& operator=(const sharedptr<T>& sp) //賦值運(yùn)算符的重載{if (_ptr != sp._ptr){delete _ptr;delete _refcount;_ptr = sp._ptr;_refcount = sp._refcount;++(*_refcount);}return *this;}~sharedptr() //析構(gòu){Realease();}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}T Getrefcount(){return *(_refcount);}inline void Realease(){if (--*_refcount == 0){delete _refcount;delete _ptr;}}void Reset(T* ptr ,T* refcount){if (_ptr != ptr){delete _ptr;delete _refcount;}_ptr = ptr;_refcount = refcount;} public:T* _ptr;T* _refcount;//T _refcount;//有缺陷//int static _refcount;//有缺陷 }; void test() {sharedptr<int> s1(new int(10));//cout << *s1._refcount << endl;cout << s1.Getrefcount() << endl;sharedptr<int> s2(s1);//cout << *s2._refcount << endl;cout << s2.Getrefcount() << endl;sharedptr<int> s3(new int(20));s3 = s1;//cout << *s3._refcount << endl;cout << s3.Getrefcount() << endl; } int main() {test();//*sharedptr<int> sp; // 驗(yàn)證Reset//sp.Reset(new int,new int(1)); //*sp = 10;//cout << *sp << endl;//sp.Reset(new int, new int(1)); //*sp = 20;//cout << *sp << endl;//sp.Reset(); system("pause");return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96


總結(jié)

以上是生活随笔為你收集整理的C++智能指针(二)模拟实现三种智能指针的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。