четверг, 5 мая 2011 г.

Умные указатели

Когда то я сделал свой класс для умных указателей , надеюсь для кого нить это будет интересно и
полезно.


template<class T> class Holder{
mutable T *obj;
public:
Holder<T>()throw():obj(0) {}
Holder<T>(T *x) throw():obj(x){}
Holder<T>(const Holder<T> & q) throw(){obj=q.obj;q.obj=0;}
Holder<T> & operator=(const Holder<T> & q) throw(){
if (obj) 
dbgprintf(_T("Holder d'nt free object"));
obj=q.obj;q.obj=0;return *this;}
Holder<T> & operator=(T* q) throw(){
if (obj) 
dbgprintf(_T("Holder d'nt free object"));
obj=q;return *this;}
//AG212 ~Holder<T>(){if (obj) delete obj;}
  ~Holder<T>(){if (obj) delete obj; obj=NULL;}
inline void release() throw() {obj=0;}
inline operator T*() throw(){return obj;}
inline operator const T*() const throw(){return obj;}
inline T* value() throw(){return obj;}
inline const T* const_value() const throw(){return obj;}
inline T* operator->() throw(){return obj;}
inline const T* operator->() const throw(){return obj;}
inline void free(){if (obj) delete obj;obj=0;}
inline bool isEmpty() const throw(){return !obj;}
inline bool empty() const throw(){return !obj;}
inline bool operator!() const throw(){return !obj;}
inline operator bool()const throw(){return obj!=0;}
};

Комментариев нет:

Отправить комментарий