Part2:CSmartPtr

Part2中的智能指针:

#ifndef _SMARTPTR_H_
#define _SMARTPTR_H_

#include <cassert>

#ifndef NULL
#define NULL	0
#endif

// A template class that provides smart pointer functionality.
/*
   The principle is that each instance of this class holds a pointer to a structure
   that contains a reference counter of the object and the pointer to the object itself.
   The reference is incremented each time the smart pointer is copied and decremented 
   each time the smart pointer is destroyed.
   When no one reference the pointer anymore, the pointed object is destroyed.
*/
template <class T>
class CSmartPtr
{
public:
	//! Default constructor. Initializes the pointer to NULL
	CSmartPtr()
	{
		m_pPtrInfo = NULL;
	}
	// Contructor with a pointer to track. pPointee is the pointer to keep 
	// track of. Note that the smart pointer will keep ownership of this 
	// pointer and will delete it itself, so don't delete it  yourself.
	CSmartPtr(T* pPointee)
	{
		m_pPtrInfo = new TSharedData;
		m_pPtrInfo->m_pPointee = pPointee;
		m_pPtrInfo->m_iRefCount = 1;
	}
	// Copy constructor. The argument is a smart pointer to make a copy of. 
	// The smart pointer will increment the reference counter of the pointed object.
	CSmartPtr(const CSmartPtr& other)
	{
		m_pPtrInfo = other.m_pPtrInfo;
		if (m_pPtrInfo)
			m_pPtrInfo->m_iRefCount++;
	}
	// Assignment operator. The argument is a smart pointer to make a copy of. 
	// The smart pointer will increment the reference counter of the pointed 
	// object. If the smart pointer was already tracking a variable, the reference 
	// counter for this variable will be decremented (and the pointer destroyed 
	// if it becomes 0).
	CSmartPtr& operator=(const CSmartPtr& other)
	{
		if (this != &other)
		{
			if (m_pPtrInfo)
			{
				m_pPtrInfo->m_iRefCount--;
				if (m_pPtrInfo->m_iRefCount == NULL)
				{
					delete m_pPtrInfo->m_pPointee;
					delete m_pPtrInfo;
				}
			}
			m_pPtrInfo = other.m_pPtrInfo;
			if (m_pPtrInfo)
				m_pPtrInfo->m_iRefCount++;
		}
		return *this;
	}
	// Destructor. It decrements the shared reference counter. 
	// If it becomes 0, the pointed variable is destroyed.
	~CSmartPtr()  
	{ 
		if (m_pPtrInfo)
		{
			m_pPtrInfo->m_iRefCount--;
			if (m_pPtrInfo->m_iRefCount == 0)
			{
				delete m_pPtrInfo->m_pPointee;
				delete m_pPtrInfo;
			}
		}
	}


	// Overloading of the * operator to access the contents of the pointed variable.
	T& operator* () const  
	{ 
		assert(m_pPtrInfo != NULL);
		return *(m_pPtrInfo->m_pPointee); 
	}
	// Overloading of the -> operator that returns the pointer to the variable.
	T* operator->() const  
	{ 
		assert(m_pPtrInfo != NULL);
		return m_pPtrInfo->m_pPointee; 
	}

	// Check to see if the pointer to the variable is NULL.
	bool isNull() const  
	{ 
		if (m_pPtrInfo && m_pPtrInfo->m_pPointee)
			return false;
		return true;
	}

private:
	// Structure shared across smart pointers.
	struct TSharedData
	{
		T*	m_pPointee;
		int m_iRefCount;
	};
	TSharedData* m_pPtrInfo;
};

#endif  // _SMARTPTR_H_

简单使用:

        typedef CSmartPtr<CImage> TImagePtr;

        TImagePtr imgPtr(new CImage(strFileName));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值