#include "Singleton.h" //========================================================================= // Singleton class implementation // only the default constructor is implemented as it is the only constructor // that will be used so we will not get "undefined references" for the copy // constructor and assignment operator when linking // SingletonDestroyer Singleton :: destroyer; //------------------------------------------------------------------------- // initialize the pointer to the single isntance Singleton* Singleton::instance = 0; //------------------------------------------------------------------------- // default constructor (just initialises imlp) Singleton::Singleton() { } //------------------------------------------------------------------------- // getInstance: // if NULL construct the sinle instance, otherwise just return it Singleton* Singleton::getInstance() { // is it the first call? if (instance == 0) { // create sole instance instance = new Singleton; Singleton :: destroyer.setSingleton(instance); } // return the address of sole instance return instance; } Singleton::~Singleton() { } //========================================================================= // SingletonDestroyer class implementation SingletonDestroyer::~SingletonDestroyer() { delete instance; } void SingletonDestroyer::setSingleton( SingletonPtr _instance) { instance = _instance; }