auto_ptr (防止内存泄露)代码赏析

 C++ |  copy code |? 
001
template<class _Ty>
002
 class auto_ptr
003
 { // wrap an object pointer to ensure destruction
004
public:
005
 typedef _Ty element_type;
006
 
007
 explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()
008
 : _Myptr(_Ptr)
009
 { // construct from object pointer
010
 }
011
 
012
 auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()
013
 : _Myptr(_Right.release())
014
 { // construct by assuming pointer from _Right auto_ptr
015
 }
016
 
017
 auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
018
 { // construct by assuming pointer from _Right auto_ptr_ref
019
 _Ty *_Ptr = _Right._Ref;
020
 _Right._Ref = 0; // release old
021
 _Myptr = _Ptr; // reset this
022
 }
023
 
024
 template<class _Other>
025
 operator auto_ptr<_Other>() _THROW0()
026
 { // convert to compatible auto_ptr
027
 return (auto_ptr<_Other>(*this));
028
 }
029
 
030
 template<class _Other>
031
 operator auto_ptr_ref<_Other>() _THROW0()
032
 { // convert to compatible auto_ptr_ref
033
 _Other *_Cvtptr = _Myptr; // test implicit conversion
034
 auto_ptr_ref<_Other> _Ans(_Cvtptr);
035
 _Myptr = 0; // pass ownership to auto_ptr_ref
036
 return (_Ans);
037
 }
038
 
039
 
040
 template<class _Other>
041
 auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
042
 { // assign compatible _Right (assume pointer)
043
 reset(_Right.release());
044
 return (*this);
045
 }
046
 
047
 template<class _Other>
048
 auto_ptr(auto_ptr<_Other>& _Right) _THROW0()
049
 : _Myptr(_Right.release())
050
 { // construct by assuming pointer from _Right
051
 }
052
 
053
 auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
054
 { // assign compatible _Right (assume pointer)
055
 reset(_Right.release());
056
 return (*this);
057
 }
058
 
059
 auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
060
 { // assign compatible _Right._Ref (assume pointer)
061
 _Ty *_Ptr = _Right._Ref;
062
 _Right._Ref = 0; // release old
063
 reset(_Ptr); // set new
064
 return (*this);
065
 }
066
 
067
 ~auto_ptr()
068
 { // destroy the object
069
 if (_Myptr != 0)
070
 delete _Myptr;
071
 }
072
 
073
 _Ty& operator*() const _THROW0()
074
 { // return designated value
075
 
076
 #if _HAS_ITERATOR_DEBUGGING
077
 if (_Myptr == 0)
078
 _DEBUG_ERROR("auto_ptr not dereferencable");
079
 #endif /* _HAS_ITERATOR_DEBUGGING */
080
 
081
 __analysis_assume(_Myptr);
082
 
083
 return (*get());
084
 }
085
 
086
 _Ty *operator->() const _THROW0()
087
 { // return pointer to class object
088
 
089
 #if _HAS_ITERATOR_DEBUGGING
090
 if (_Myptr == 0)
091
 _DEBUG_ERROR("auto_ptr not dereferencable");
092
 #endif /* _HAS_ITERATOR_DEBUGGING */
093
 
094
 return (get());
095
 }
096
 
097
 _Ty *get() const _THROW0()
098
 { // return wrapped pointer
099
 return (_Myptr);
100
 }
101
 
102
 _Ty *release() _THROW0()
103
 { // return wrapped pointer and give up ownership
104
 _Ty *_Tmp = _Myptr;
105
 _Myptr = 0;
106
 return (_Tmp);
107
 }
108
 
109
 void reset(_Ty* _Ptr = 0)
110
 { // destroy designated object and store new pointer
111
 if (_Ptr != _Myptr && _Myptr != 0)
112
 delete _Myptr;
113
 _Myptr = _Ptr;
114
 }
115
 
116
private:
117
 _Ty *_Myptr; // the wrapped object pointer
118
 };

文章分类 Classic Code

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>