引用计数类模板


template<typename T>
class CRSIRefCnt
{
public:
	CRSIRefCnt() :
	  m_pstObject(NULL)
	  {
	  }

	  CRSIRefCnt(T* pstObject) :
	  m_pstObject(pstObject)
	  {
		  if(m_pstObject)
		  {
			  m_pstObject->AddRef();
		  }
	  }

	  CRSIRefCnt(const CRSIRefCnt<T>& stOther) :
	  m_pstObject(stOther.m_pstObject)
	  {
		  if (m_pstObject)
			  m_pstObject->AddRef();
	  }

	  ~CRSIRefCnt()
	  {
		  if (m_pstObject)
			  m_pstObject->Release();
	  }

	  CRSIRefCnt<T>& operator=(const CRSIRefCnt<T>& stOther)
	  {
		  if (this != &stOther && *this != stOther)
		  {
			  if (m_pstObject)
				  m_pstObject->Release();

			  m_pstObject = stOther.m_pstObject;

			  if (m_pstObject)
				  m_pstObject->AddRef();
		  }

		  return *this;
	  }

	  bool operator==(const CRSIRefCnt<T>& stOther) const
	  {
		  return m_pstObject == stOther.m_pstObject;
	  }

	  bool operator!=(const CRSIRefCnt<T>& stOther) const
	  {
		  return m_pstObject != stOther.m_pstObject;
	  }

	  const T& operator*() const
	  {
		  return *m_pstObject;
	  }

	  T& operator*()
	  {
		  return *m_pstObject;
	  }

	  const T* operator->() const
	  {
		  return m_pstObject;
	  }

	  T* operator->()
	  {
		  return m_pstObject;
	  }

	  bool IsValid() const
	  {
		  return m_pstObject != NULL;
	  }

private:
	T* m_pstObject;
};


相关文章:

  • 2021-06-21
  • 2021-10-22
  • 2021-12-30
  • 2021-11-16
  • 2021-11-22
  • 2021-12-04
  • 2021-12-22
猜你喜欢
  • 2022-12-23
  • 2021-11-28
  • 2021-05-02
  • 2021-12-16
  • 2022-01-14
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案