This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: problems with undefined references
- From: Gokhan Kisacikoglu <kisa at centropolisfx dot com>
- To: Brad Douglas <brad dot douglas at saabsystems dot com dot au>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 25 Jun 2002 10:08:17 -0700
- Subject: Re: problems with undefined references
- Organization: Centropolis Effects, LLC
- References: <7DA8A54A920E1441960C129BDDFE4E9A042390@dr-honeydew.saabsystems.com.au>
- Reply-to: kisa at centropolisfx dot com
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;
}