This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problems with undefined references


Here I fixed your source code and attached it. You did not declare the
static variable anywhere, it is also safer to call the static variable
with its scope.

Gokhan
#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;
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]