#pragma once template class KSmartPtr { public: explicit KSmartPtr(T* pObj = NULL) { m_pObj = pObj; if(m_pObj) m_pObj->AddRef(); } ~KSmartPtr() { if(m_pObj) m_pObj->Release(); } /// Copy constructor KSmartPtr(const KSmartPtr& rhs) { m_pObj = rhs.m_pObj; if(m_pObj) m_pObj->AddRef(); } /// Assign KSmartPtr& operator=(const KSmartPtr& rhs) { _Copy(rhs.m_pObj); return *this; } KSmartPtr& operator=(T* pObject) { _Copy(pObject); return *this; } /// Conversion operator const T*() const { return m_pObj; } const T* operator->() const { return m_pObj; } const T& operator*() const { return *m_pObj; } operator T*() { return m_pObj; } T* operator->() { return m_pObj; } T& operator*() { return *m_pObj; } /// Compare bool operator==(const KSmartPtr& Ptr) const { return m_pObj == Ptr.m_pObj; } bool operator!=(const KSmartPtr& Ptr) const { return m_pObj != Ptr.m_pObj; } private: void _Copy(T* pObj) { if(pObj != m_pObj) { if(m_pObj) m_pObj->Release(); m_pObj =pObj; if(m_pObj) m_pObj->AddRef(); } } private: T* m_pObj; }; #define DECL_SPTR(ClassName)\ typedef KSmartPtr ClassName##SPtr;