Skip to main content
GameDev.net gamedev.net
🔒 Locked

Singleton Static Member... How To Initialize?

Started by Raeldor Oct 16, 2005 at 6:03 PM 5 replies 400+ views
Original Post
Raeldor
Raeldor
I need help to explain the singleton implementation. From what I understand you have a class that has a static variable that is a pointer to the same class type. You them make the constructor private and initialize the class by calling a static function similar to...

static MyClass* CreateMyClass()
{
if (m_singleton == NULL)
return new MyClass();
else
return m_singleton;
}

This seems fine, but how can you guarantee that the m_singleton member will be NULL? Have I got this right? Am I missing something here?
smitty1276
smitty1276
// MySingleton.hppclass MySingleton{protected:    static MySingleton *INSTANCE;    // Constructor (private)           MySingleton();public:    static MySingleton *getInstance();};// MySingleton.cpp#include "MySingleton.hpp"MySingleton *MySingleton::INSTANCE = NULL;MySingleton::MySingleton(){    //do stuff here}MySingleton *MySingleton::getInstance(){    if (INSTANCE == NULL)        INSTANCE = new MySingleton();    return INSTANCE;}
Raeldor
Raeldor
Aha, thank you... I guess this is the key piece of code I was missing...

MySingleton *MySingleton::INSTANCE = NULL;

I have never seems static members initialized outside the class like this. How does this work? Where is the static variable held in memory? Is it held in the same area as the rest of the class variables? Does an instance of the class get created when you initialize the static like this? I am guessing not.

Can the static variable be initialized in the class declaration like...

    static MySingleton *INSTANCE=NULL;

?

Thanks
Fruny
Fruny
Quote:
Original post by Raeldor
I have never seems static members initialized outside the class like this. How does this work? Where is the static variable held in memory? Is it held in the same area as the rest of the class variables? Does an instance of the class get created when you initialize the static like this? I am guessing not.


A static member variable is like a global variable, except it is only accessible through the class interface. There is only one copy of the variable, which doesn't belong to any instances of the class, but rather to the class itself.

Quote:
Can the static variable be initialized in the class declaration like...


Only if it is a static, integral constant member.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
hplus0603
hplus0603
Actually, a more common implementation of singleton is to use a global accessor function, and make the singleton instance a static in that function. It's still not thread safe, but the class doesn't have to expose the static member, and you can even create more singletons of the same kind.

// MyClass.hclass MyClass {  public:    // public interface goes here};extern MyClass & GlobalInstance();extern MyClass & AnotherGlobalInstance();// MyClass.cppclass ConcreteMyClass : public MyClass {  public:    // implementation goes here, if not provided in MyClass.h};MyClass & GlobalInstance() {  static ConcreteMyClass it(1); // will be created on first call  return it;}MyClass & AnotherGlobalInstance() {  static ConcreteMyClass it(2); // will be created on first call  return it;}


The benefit is that you don't need to show messy implementation details in the header, and you can have more than one instance of the singleton if necessary.
enum Bool { True, False, FileNotFound };
Raeldor
Raeldor
That's an interesting implementation. In the code...

MyClass & GlobalInstance() {  static ConcreteMyClass it(1); // will be created on first call  return it;}

What is the (1) used for? Is this assuming the constructor for the class takes a parameter?
Omaha
Omaha
Being able to create multiple instances... that sort of eliminates the purpose of a singleton, now, doesn't it? It seems to me you now have a normal class with a factory function.

The singleton is one particular pattern of implementing a specific data structure. There is no set way to do it, so don't be so concerned about doing it the "right" way. The point of it is that you enforce a single instance.

Personally, I'm more of a fan of the:

class CWhatever{     private:          static CWhatever *Instance;     // initialized to NULL in source file     CWhatever();     public:     CWhatever *GetInstance();     // if(!instance) instance=new whatever return instance blah blah}


"classic" strategy but I generally dislike singletons in languages like C++ that offer object oriented capabilities but are not STRICTLY object oriented, like Java. It makes much more sense to use static modules for things which you do not wish to be redundantly instantiated, but hey, whatever works for you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.