This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Smart Pointer Class
Submitted by

A lot of the nasty C++ runtime errors occur because of pointer logic errors. A
good way to avoid many of them is to start using smart pointers. For those of
you who haven't heard of them, smart pointers are basically classes designed to
act and be used like pointers, but which do more stuff and can take care of a
lot of problems. For example, smart pointers save you a lot of typing and
thinking about handling exceptional situations in try{ } catch { cleanup } type
things.Take a look at www.boost.org (specifically
here) and other places. You also have
some smart pointer classes declared in your <memory> header, which you can read
more about here: http://ootips.org/yonat/4dev/smart-pointers.html.
When you read these documents, especially the latter one I mentioned, you see a
lot of different pointer types, each of which does a different thing. Sometimes
it helps to roll your own classes, and while coding my engine Aurora, website is not ready yet ), I have coded a bunch of
components for use in the engine, some merely for practice, others with
immediate practical uses in mind. One of these was auAccessor, which has been
good for both practice and practical purposes :-)
The auAccessor class is technically a reference-linked smart pointer. I decided
to write a smart pointer that could take care of more problems for me than other
smart pointers I've seen. Here are some of its capabilities:

It throws an error when you try to access NULL memory.

It creates new objects (by calling New() method ).

It maintains doubly linked lists of auAccessors pointing to the same object.

These lists are managed behind the scenes, as objects are created and destroyed,
pointers are reassigned, and so on. Garbage collection, etc. is automatic.

It is non-intrusive (doesn't add data members to the object.)

It can be reassigned to point to another object.

You can call Delete() method to delete the object pointed to, and this will not
only delete it but also setall the auAccessors still pointing to the deleted
object to point to NULL. They will then throw if misused.

Ability to Copy() the object, creating a new object, a copy, to which the
auAccessor will now point.

And other stuff...

As you can see, this smart pointer performs most of the tasks other smart
pointers do, and avoids a lot of problems, such as trying to use an object after
it has been deleted. In my opinion it's better than copy on write pointers
because it gives you the option to copy instead of forcing it on you, it
provides more intuitive, less simple solutions than auto_ptr does. In any case,
I rolled my own class and here's what I came up with. Maybe it will be useful to
you.

The code for the class is below.
Warning: the file below will not compile in your project straight away. You will
have to change one or two minor things in it. For example, instead of throwing
new auError, which is an error class in my engine, you can throw some other
object which you define.

This class does add a little overhead to functions such as assignment of a
pointer, which would otherwise have virtually no overhead. But it's always
constant-time overhead (except for setting a pointer = 0) and in fact I would
say most of it is just doing the "clean programming" which you would code anyway
if you had the time. You can add more stuff if you like to this class, like a
function returning the length of the array or record allocated, and so on.

If you find any errors or have any ideas, comments, suggests or threats, let me
know. You can contact me at contactgreg@hotmail.com. Tell me how you like it,
in fact. I want to hear from you! You can also comment and banter below. :-)