The
C++ STL (Standard
Template Library) implementation of a
generic smart pointer. Utilizes the
C++ feature of a class
destructor to the automatically free
dynamically allocated
memory of the
object to which it
references.
class Foo
{
public:
Foo(int val) : bar(val) {}
int GetBar() const {return bar;}
protected:
int bar;
};
void print3()
{
//construct fooPtr with pointer to new Foo object
std::auto_ptr fooPtr = new Foo(3);
//access referenced object as if fooPtr were a regular pointer
cout << "bar = " << fooPtr->GetBar() << endl;
//Foo object referenced by fooPtr is automatically freed as fooPtr goes out of scope
}