= smart pointer = {{{~cpp #ifndef _SMARTPTR_H #define _SMARTPTR_H // reference countable smart point // circular member setting will not free memory // // eg) // CA has SmartPtr member and CB has SmartPtr member. // In other word, each object has the other object's smart pointer. // CA will be freed after CB is freed, and vise versa. // Memory will not be freed forever. template class SmartPtr { struct RefPtr { _Ty *_Ptr; int _Refcnt; }; public: typedef _Ty element_type; explicit SmartPtr(_Ty *_P = 0) throw () { if(_P != 0) { _Ptr = new RefPtr; _Ptr->_Ptr = _P; _Ptr->_Refcnt = 0; IncRefcnt(); } else _Ptr = 0; } SmartPtr(const SmartPtr<_Ty>& _Y) throw () : _Ptr(_Y._Ptr) { IncRefcnt(); } SmartPtr<_Ty>& operator=(const SmartPtr<_Ty>& _Y) throw () { if (this != &_Y) { if (_Ptr != _Y._Ptr) { DecRefcnt(); _Ptr = _Y._Ptr; IncRefcnt(); } } return (*this); } ~SmartPtr() { DecRefcnt(); } _Ty& operator*() const throw () {return (*get()); } _Ty *operator->() const throw () {return (get()); } _Ty *get() const throw () {return (_Ptr->_Ptr); } _Ty *release() throw () { _Ty *_tPtr = _Ptr->_Ptr; if(_Ptr) { --(_Ptr->_Refcnt); if(_Ptr->_Refcnt <= 0) { } _Ptr = NULL; } return (_tPtr); } bool operator !() { if(_Ptr == 0) return TRUE; return FALSE; } template operator SmartPtr<_OTy>() { return SmartPtr<_OTy> (_Ptr); } private: void IncRefcnt() { if(_Ptr) ++(_Ptr->_Refcnt); } void DecRefcnt() { if(_Ptr) { --(_Ptr->_Refcnt); if(_Ptr->_Refcnt <= 0) { delete _Ptr->_Ptr; _Ptr->_Ptr = NULL; delete _Ptr; } _Ptr = NULL; } } private: RefPtr *_Ptr; }; #endif }}} = thread = 이런게 있기 때문에 C++ 이 재미있는게 아닐까? - [eternalbleu] 이런걸 안써도 되어서 Python이 재미있는 것일지도. (하지만 Extending 쪽에서는 결국 써야 하는.. 흑) --[1002]