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 Template Class
Submitted by

This code snippet is from my upcoming book "Game Engine Design and Implementation using DirectX 8.1" and the
contained "Realspace" game engine. It's a smart pointer template that has undergone a lot of testing.
It mirrors the behaviour of normal pointers as closly as possible, however does not support the following things:
*Pointers to base classes in class hierarchies
*Never create a smartpointer from an object allocated on the stack like Pointer p( &someClass );
because it could crash your programm since the memory would be freed as soon as the Pointer goes out of focus
Everything else should work fine, and allows you to have multiple references to the same object which is automatically deleted when no objects
refer it anymore.
Also the class frees you of the troubles of NULL Pointer checking since that is done for you ( slows everything down, but you could blend it out in release builds )
You can use this code at your absolute free will..
Hope anyone finds it useful!
Bernhard Glück
PS: I would like to hear suggestions, bug fixes or anything, and I am looking for some skilled graphics artists willing to help me out on my example game
using the "Realspace" Engine (it's a space game")

// -----------------------------------------------------------------------------
// File contents / class description:
// -----------------------------------------------------------------------------
//
// Smart pointer template class
//
// -----------------------------------------------------------------------------
// Originally created on 05/15/2000 by Bernhard Glueck
// Email: jglueck@vol.at
// You can use this software at your own free will, however I am not to be held
// responsible for any damage this does to you or to your health :-)
// You can freely use the code provided in your own programs, however if you find
// any bugs, or add a cool new feature to it, I would like to hear about it.
// -----------------------------------------------------------------------------
#ifndef __POINTER_H__
#define __POINTER_H__

/// Disable UDT warning
#pragmawarning ( disable : 4284 )

// -----------------------------------------------------------------------------
/// Error handling macro only for this version
#define ErrorReport( a )