Archived

This topic is now archived and is closed to further replies.

Singleton classes, how?

This topic is 5013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i want to use a Singleton class, but not sure on how to go about creating one. here is what ive got so far:
//c++ source

class MySingleton {
private:
	static MySingleton *myReference;
	MySingleton()
	{
	}

public:
	static MySingleton *getInstance()
	{
		if(myReference==NULL)
			myReference = new MySingleton();

		return myReference;
	}
};
which is basically from copying a java example that ive been using at tafe. heres the java if u want to see
//java source

public class SingletonClass {
  private static SingletonClass singletonReference;

  public SingletonClass() {
  }

  public static SingletonClass getInstance()
  {
    if(singletonReference==null)
      singletonReference = new SingletonClass();

      return singletonReference;
  }
}
But iam using c++ so i dont think the translation is totally correct. So is that singleton class correct, i think i need to: 1) delete myReference somewhere 2) declare the static myReference somehow, i keep getting private error or something when i try that and basically i dont know how to run it either, ie how do i run the getInstance()? thanks,

Share this post


Link to post
Share on other sites
< WARNING: ROGUE CODE FOLLOWS >

The way *i* do it is:

1) make de/constructors private
2) make everything else static

The whole "GetInstance()" garbage is a waste of typing IMHO unless someone can say otherwise. The Singleton really doesn't need a presence of any form (that i know of, again i could be wrong)

So, instead of

Class::GetInstance()->DoSomething()

Just have DoSomething() be static:

class Class {
static void DoSomething();
}

and call it like so:

Class::DoSomething();

Make all functions and all class data members static. That's how i've always done it and it's always worked simple and well.

[edited by - leiavoia on March 26, 2004 11:46:59 AM]

Share this post


Link to post
Share on other sites
The other way could be


//c++ source

class MySingleton {
private:
MySingleton()
{
}

public:
static MySingleton& getInstance()
{
static MySingleton instance;
return instance;
}
};


No need for a static member in this case

Share this post


Link to post
Share on other sites

//c++ sourceclass

MySingleton
{
private:
MySingleton()
{ }

public:
static MySingleton& getInstance()
{
static MySingleton* instance = new MySingleton;
return *instance;
}
};


That is a lot better because it's not subject to undefined destruction order across translation units. Don't worry about deleting the pointer, it's not a leak. This singleton should be avaible for the lifetime of the process and just let the runtime free the memory when your app gets killed.

[edited by - fallenang3l on March 26, 2004 1:21:16 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by fallenang3l
Don''t worry about deleting the pointer, it''s not a leak. This singleton should be avaible for the lifetime of the process and just let the runtime free the memory when your app gets killed.



That seems like really bad practice to me. By that logic, we shouldn''t worry about memory leaks anywhere outside of loops. The runtime will just clean it up, right?

Technically, yes, the runtime will just kill it when it kills the process. But it just seems wrong.

I like pie.

Share this post


Link to post
Share on other sites
quote:
Original post by T1mb0
The other way could be


//c++ source

class MySingleton {
private:
MySingleton()
{
}

public:
static MySingleton& getInstance()
{
static MySingleton instance;
return instance;
}
};




quote:
Original post by fallenang3l

//c++ sourceclass

MySingleton
{
private:
MySingleton()
{ }

public:
static MySingleton& getInstance()
{
static MySingleton* instance = new MySingleton;
return *instance;
}
};





hi on both these examples i am just curoious on how they prevent the user from create any more instances, like they don''t seem to have anything in there to stop that.

Im quite new to singleton classes so excuse my noobiness

Share this post


Link to post
Share on other sites
This is a simple and effective implementation; technically it''s not 100% perfect code, but it does work properly. (If you use mutliple threads, get_instance needs a lock).


struct MySingleton
{
static MySingleton* get_instance()
{
static MySingleton* instance=0;
if(instance==0)
instance = new MySingleton;
return instance;
}
private:
MySingleton(){}
~MySingleton(){}
};



That said, every single time I have ever used a singleton or global objects, I have regetted it and changed it later.

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo
quote:
Original post by T1mb0
The other way could be
//



hi on both these examples i am just curoious on how they prevent the user from create any more instances, like they don''t seem to have anything in there to stop that.

Im quite new to singleton classes so excuse my noobiness




The constructor is private so another instance can''t be created.

#define GMySingleton MySingleton::getInstance()
is the only instance that can be used now. (Or you can use inline just so I don''t get bombed )

-UltimaX-
Ariel Productions
|Designing A Screen Shot System|

"You wished for a white christmas... Now go shovel your wishes!"

Share this post


Link to post
Share on other sites