⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsshareptr.h

📁 连连看这个游戏都玩过吧
💻 H
字号:
// SharePtr.h: interface for the TSharePtr class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SHAREPTR_H__404CB0B2_E4D5_4DA6_AEF4_9E1047B96E7C__INCLUDED_)
#define AFX_SHAREPTR_H__404CB0B2_E4D5_4DA6_AEF4_9E1047B96E7C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


template <class type>
class GSLIB_API TSharePtr  
{
	type**	m_pptr;
	int*	pref;
public:
	type*	get() const					{ return *m_pptr; }
	int		getref() const				{ return *pref; }
	int		Release()					
	{
		if(pref==NULL || *pref<=0)
			return 0;
		else if(*pref==1)
		{
			delete *m_pptr;
			delete m_pptr;
			delete pref;
			pref	= NULL;
			m_pptr	= NULL;
			return 0;
		}
		*pref--;
		return *pref;	
	}

	TSharePtr& operator=( const TSharePtr<type> ptr )	
	{ 
		Release();
		m_pptr	= ptr.m_pptr;
		pref	= ptr.pref;
		if(ptr.pref)
		{
			*pref++;
		}
	}
/*
	//this will construct a new object for the ptr
	TSharePtr& operator=( type* data )	
	{
		Release();
		pref	= NULL;
		m_pptr	= NULL;
		if(data!=NULL)
		{
			pref	= new int;
			*pref=1;
			m_pptr	= new type;
			*m_pptr	= *data;
		}
		return *this;
	}
*/	
	type*         operator->() const	{ return get(); }
	type&         operator*() const		{ return *get(); }
	bool          operator!() const		{ return get() != 0 && pref!=0 && *pref>0; }


	bool operator==( const type* right ) const
	{
		return get() == right;
	}

	bool operator!=( const type* right ) const
	{
		return get() != right;
	}

	TSharePtr()							
	{ 
		m_pptr=new type*;
		*m_pptr= new type;
		pref=new int;
		*pref=1;
	}
	TSharePtr( const TSharePtr<type> &ptr )
	{
		m_pptr	= ptr.m_pptr;
		pref	= ptr.pref;
		if(ptr.pref)
		{
			*pref++;
		}
	}
	~TSharePtr()						{ Release(); }
};

#endif // !defined(AFX_SHAREPTR_H__404CB0B2_E4D5_4DA6_AEF4_9E1047B96E7C__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -