00001
00002
00003
00004
00005
00006 #ifndef __AUTO_PTR_H__
00007 #define __AUTO_PTR_H__
00008
00009 template<class X>
00010 class auto_ptr {
00011 public:
00012 explicit auto_ptr(X *p = 0) : _data (p) {}
00013
00014 template <class U>
00015 auto_ptr(auto_ptr<U> &rhs) : _data(rhs.release()) {}
00016
00017 ~auto_ptr() { delete _data; }
00018
00019 template<class U>
00020 auto_ptr<X>& operator=(auto_ptr<U> &rhs) {
00021 if (this != &rhs) reset(rhs.release());
00022 return *this;
00023 }
00024
00025 X& operator*() const { return *_data; }
00026
00027 X* operator->() const { return _data; }
00028
00029 X* get() const { return _data; }
00030
00031 X* release() {
00032 X *old_data = _data;
00033 _data = 0;
00034 return old_data;
00035 }
00036
00037 void reset(X *p = 0) {
00038 if (_data != p) {
00039 delete _data;
00040 _data = p;
00041 }
00042 }
00043
00044 private:
00045 X *_data;
00046 };
00047
00048 #endif // __AUTO_PTR_H__