digital adj. Having digits.     peer n. A comrade; a companion; a fellow; an associate. inmotion    
   
Recent Articles
Stop DNSMasq From Forwarding Local Hostnames
Saturday, September 25, 2010
Securing your Wireless LAN
Wednesday, August 18, 2010
Some tips and things you might not know about your wireless network.
Using Different Subversion Client Versions
Wednesday, August 18, 2010
Handling a Subversion Repository URL Change
Sunday, May 3, 2009
If your repository URL changes, you can use the following command to fix existing snapshots.
vfat Mounts Default to Lowercase Shortnames
Tuesday, April 21, 2009
I want a "this is brain-damage" quote from Linus for this mess.
VirtualBox or VMWare Virtual Machine at Login
Sunday, April 12, 2009
How to start a virtual machine in X when a user logs in.
Dialog Progress Bar Through Pipe
Sunday, April 12, 2009
How to use dialog to display a script progress bar and communicate progress to it through a named pipe.
Mount JFFS2 Image
Saturday, October 25, 2008
Example of how to mount a JFFS2 image using mtdblock.

There is no such thing as a boring project. There are only boring executions.
- Irene Etzkorn

Projects-Development...-C/C++ Guide-Smart...

Smart Pointer Class

Tuesday, July 13, 2004 by digitalpeer, updated Wednesday, August 18, 2004

Smart pointers were the latest craze awhile back, but in my opinion are sometimes more trouble than their worth. The idea is to use pointers and never have to de-allocate what they are pointing to. They use reference counting to delete the object when the pointer finally goes out of scope.

This is a simple "smart pointer" class I wrote that might help assist you in developing your own or even understanding how they work.
#ifndef SMARTPTR_H
#define SMARTPTR_H

#include <cassert>
using namespace std;

template <typename type>
class smartptr
{
	public:
		smartptr(type* ptr = 0);
		smartptr(const smartptr<type>& orig);
		~smartptr();

		type* operator->() const;
		type& operator*() const;
		smartptr<type>& operator=(const smartptr<type>& rhs);


	private:
		/* The pointer to the object we created */
		type* thePointer_;
		/* The number of instances pointing to this pointer */
		int* count_;
		
		void doDelete();
};

template <typename type>
smartptr<type>::smartptr(type* ptr):thePointer_(ptr), count_(new int)
{
	*count_ = 1;
}

template <typename type>
smartptr<type>::smartptr(const smartptr<type>& orig):thePointer_(orig.thePointer_)
{
	count_ = orig.count_;
	thePointer_ = orig.thePointer_;
	++(*count_);
}

template <typename type>
smartptr<type>::~smartptr()
{
	doDelete();
}

template <typename type>
type* smartptr<type>::operator->() const
{
	return thePointer_;
}

template <typename type>
type& smartptr<type>::operator*() const
{
	assert(thePointer_ != 0);
	return *thePointer_;
}

template <typename type>
smartptr<type>& smartptr<type>::operator=(const smartptr<type>& rhs)
{
	/* check for a=a */
	if (this != &rhs)
	{
		doDelete();
		count_ = rhs.count;
	}

	return *this;
}

template <typename type>
void smartptr<type>::doDelete()
{
	assert(*count_ != 0);
	*count_ = *count_ - 1;
	if (*count_ == 0)
	{
		delete thePointer_;
		delete count_;
	}
}

#endif


Submit Comment to This Article - Be the first!
Please post a comment if you have something to add, find something wrong, or would like more information on the topic at hand. Do not use the comment form to contact the author about unrelated concerns!

Name: Email (optional):
Enter verification number here: