Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

take a look this smart pointer.

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#ifndef __ptr__h_
#define __ptr__h_

/**
 * Thread-safe smart pointer.
 * T must implement addRef() and release() methods.
 * release() must destroy the object if no more references.
 * Initial "reference count" must be 0.
 */
template <class T> class Ptr
{
public:
	/** Null pointer. */
	Ptr()						{m_object = 0;}
	~Ptr()						{if ( m_object ) m_object->release();}

	/** Increments reference count and stores. */
	Ptr( const Ptr<T>& other )													
	{
		T* obj = other.ptr(); 
		if ( obj ) obj->addRef(); 
		m_object = obj;
	}
	Ptr( T* other )																
	{
		if ( other ) other->addRef(); 
		m_object = other;
	}

	/** 
	 * Releases old reference, increments new reference 
	 * count and stores. 
	 */
	Ptr<T>& operator=( const Ptr<T>& other )									
	{
		T* obj = other.ptr(); 
		if ( obj ) obj->addRef(); 
		if ( m_object ) m_object->release(); 
		m_object = obj; 
		return *this;
	}

	/** if the references point to the same unique object. */
	bool	operator==( const T* other ) const									
	{
		return m_object == other;
	}
	bool	operator==( const Ptr<T>& other ) const								
	{
		return m_object == other.m_object;
	}
	
	/** if the references point to different unique objects. */
	bool	operator!=( const T* other ) const									
	{
		return m_object != other;
	}
	bool	operator!=( const Ptr<T>& other ) const								
	{
		return m_object != other.m_object;
	}

	/** Access to the object. */
	T&		operator*() const			{return *m_object;}
	T*		operator->() const			{return m_object;}
	T*		ptr() const					{return m_object;}
	operator T*() const					{return m_object;}

private:
	T* m_object;
};
#endif // __ptr__h_


Can this work? Is it completed?

Share this post


Link to post
Share on other sites
Advertisement
It's workable, though you might want to rethink providing a conversion operator (they tend to get invoked in ways that you wouldn't expect and hide syntax errors). Also, since it's an intrusive smart pointer, you'll need to support the refcounting in the class itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It's workable, though you might want to rethink providing a conversion operator (they tend to get invoked in ways that you wouldn't expect and hide syntax errors). Also, since it's an intrusive smart pointer, you'll need to support the refcounting in the class itself.


THANKS.but what does it need to refcounting ?

Share this post


Link to post
Share on other sites
Well, look at what member functions the smart pointer class assumes the pointed-to type defines.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!