vs和vc智能指针(smart_ptr)框架源码剖析

来源:互联网 发布:神魔布袋戏 知乎 编辑:程序博客网 时间:2024/05/21 11:05

vc版本的智能指针

  1 #include<iostream>  2 using namespace std;  3  4 class Test  5 {  6 public:  7     void test()  8     {  9         cout<<"Test::test()"<<endl; 10     } 11 }; 12 13 template<class _Ty> 14 class auto_ptr 15 { 16 public: 17     //构造函数 18     explicit auto_ptr(_Ty *_P = 0):_Owns(_P != 0),_Ptr(_P) 19     {}  //必须要显式调用才行 关键字explicit 20     //析构函数 21     ~auto_ptr() 22     { 23         if(_Owns)   //如果拥有权为真 24             delete _Ptr; 25     } 26 public: 27     auto_ptr(const auto_ptr<_Ty> &_Y)//:_Owns(_Y._Owns),_Ptr(_Y.release())  //拷贝构造函数 28     { 29         _Owns = _Y._Owns; 30         _Ptr = _Y.release(); 31     } 32     auto_ptr<_Ty>& operator=(const auto_ptr<_Ty> &_Y)   //赋值构造函数 33     { 34         if(this != &_Y) //如果不是自己给自己赋值 35         { 36             if(_Ptr != _Y._Ptr) //指向的空间是否相同 37             { 38                 if(_Owns)       //拥有权为真 释放自己所指向额空间 39                     delete _Ptr; 40                 _Owns = _Y._Owns; 41             } 42             else if(_Y._Owns)   //如果_Y的拥有权为真 43             { 44                 _Owns = _Y._Owns; 45             } 46             //拥有权为假的情况 则不会将假的拥有权赋过来 47             _Ptr  = _Y.release(); 48         } 49         return *this; 50     } 51 public: 52     _Ty& operator*()const 53     { 54         return *get(); 55     } 56     _Ty* operator->()const 57     { 58         return get(); 59     } 60     _Ty* get()const 61     { 62         return _Ptr; 63     } 64     _Ty* release()  const   //const auto_ptr * const this 65     { 66         //_Owns = false; 67         ((auto_ptr<_Ty>*const)this)->_Owns = false; 68         return _Ptr; 69     } 70 private: 71     //拥有权 72     //mutable bool _Owns;       //为可变量 73     bool _Owns; 74     //指针 75     _Ty *_Ptr; 76 }; 77 78 int main() 79 { 80     /* 81     int* p = new int(10); 82     auto_ptr<int> pa(p);        //对象的释放会自动调用析构函数 83     cout<<*pa<<endl; 84 85     Test* pt = new Test; 86     auto_ptr<Test> pb(pt); 87     pb->test(); 88     */ 89     /* 90     cout<<"----------"<<endl; 91     int *p = new int(10); 92     auto_ptr<int> pa(p); 93     //auto_ptr<int> pb(pa);//析构函数被调用了两次 内存泄漏 94     auto_ptr<int> pb = pa;  //调用的也是拷贝构造函数 95     */ 96 97     int *p = new int(10); 98     int *q = new int(20); 99     auto_ptr<int>pa(p);100     auto_ptr<int>pb(q);101102     pb = pa;103     cout<<"pb = "<<*pb<<endl;104     cout<<"pa = "<<*pa<<endl;105     //拥有权的转移并不是很彻底106     return 0;107 }

vs版本

 1 #include<iostream>  2 using namespace std;  3  4 template<class _Ty>  5 class auto_ptr  6 {  7     typedef auto_ptr<_Ty> _Myt;  8 public:  9     explicit auto_ptr(_Ty *_Ptr = 0):_Myptr(_Ptr) 10     {} 11     ~auto_ptr() 12     { 13         delete _Myptr; 14     } 15 public: 16     auto_ptr(_Myt& _Right):_Myptr(_Right.release()) 17     { 18 19     } 20     _Myt& operator=(_Myt& _Right) 21     { 22         reset(_Right.release()); 23         return *this; 24     } 25     _Ty& operator*() const 26     { 27         return  (*get()); 28     } 29     _Ty* operator->()const 30     { 31         return get(); 32     } 33     _Ty* get() const 34     { 35         return (_Myptr); 36     } 37     _Ty* release()  //释放函数 38     { 39         _Ty *_Tmp = _Myptr;     //将当前指针给一个临时变量 40         _Myptr = 0;             //指向NULL 41         return _Tmp;        //返回 42     } 43     //重设函数 44     void reset(_Ty *_Ptr = 0) 45     { 46         if(_Ptr != _Myptr)  //如果设置的指针和本身不一样 47             delete _Myptr; 48         _Myptr = _Ptr; 49     } 50 private: 51     //成员为一个指针 52     _Ty *_Myptr; 53 }; 54 55 int main() 56 { 57     /* 58     int *p = new int(10); 59     auto_ptr<int> pa(p); 60     cout<<"pa = "<<*pa<<endl; 61     */ 62     int *p = new int(10); 63     int *q = new int(20); 64     auto_ptr<int> pa(p); 65     cout<<"pa = "<<*pa<<endl; 66     pa.reset(q); 67     cout<<"pa = "<<*pa<<endl; 68     auto_ptr<int> pb = pa; 69     cout<<"pa = "<<*pa<<endl;   //拥有权已经转移,无法输出,内存错误。 70     return 0; 71 }
旧版本的智能指针其拥有权的转移并不彻底,藕断丝连。而后面的版本彻底的解除了上述问题。