Dave's Brain

Browse - programming tips - smart pointer

Date: 2008may6

Q.  Which "smart pointer" should I use?

A.  I prefer STL's auto_ptr

Example use:

#include <memory>

class Thing
{
		int	a;
		int	b;
};

main()
{
	std::auto_ptr<Thing>	a(new Thing);
	std::auto_ptr<Thing>	b;

	b = a;	// Now b owns the Thing 
		
} // a and b are delete-ed when they go out of scope (here)

Using automatic (ie stack) variables is even better.

Add a comment

Sign in to add a comment
Copyright © 2008, dave - Code on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License.