Part2:CSmartPtr

本文详细介绍了智能指针的实现原理,包括其结构、构造函数、赋值运算符、析构函数以及如何安全地操作指向对象的指针。通过提供一个简单的智能指针类模板,展示了如何在C++中有效地管理内存,避免了手动管理引用计数和内存泄漏的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值