Dave's Brain

Browse - Programming Tips - Which "smart pointer" should I use?

Date: 2008may6
Language: C++

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-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.