1 // Common/AutoPtr.h 2 3 #ifndef __COMMON_AUTOPTR_H 4 #define __COMMON_AUTOPTR_H 5 6 template<class T> class CMyAutoPtr 7 { 8 T *_p; 9 public: 10 CMyAutoPtr(T *p = 0) : _p(p) {} 11 CMyAutoPtr(CMyAutoPtr<T>& p): _p(p.release()) {} 12 CMyAutoPtr<T>& operator=(CMyAutoPtr<T>& p) 13 { 14 reset(p.release()); 15 return (*this); 16 } 17 ~CMyAutoPtr() { delete _p; } 18 T& operator*() const { return *_p; } 19 // T* operator->() const { return (&**this); } 20 T* get() const { return _p; } 21 T* release() 22 { 23 T *tmp = _p; 24 _p = 0; 25 return tmp; 26 } 27 void reset(T* p = 0) 28 { 29 if (p != _p) 30 delete _p; 31 _p = p; 32 } 33 }; 34 35 #endif 36