Jump to content
  • Advertisement
Sign in to follow this  

singleton question

This topic is 5192 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

I find singleton class is just a class that have a global pointer points its object. so to access a singleton object , accessing the pointer by calling getsingleton(). so I think advantage of singleton that is a better global variable. The key of singleton is not "single". The key is singleton use "single" to get better global role.

Share this post

Link to post
Share on other sites
There can only ever be one instance of a singleton, by definition. The key of the singleton pattern is that, it is indeed single (in that only one instance of it can exist) While it is essentially just a global class, the implementation behind it is a little more thought-provoking:

class cSingleton {
cSingleton(); // notice the constructor is hidden
// thus you cannot make new instances

static cSingleton* me; // pointer to the one and only instance

cSingleton* getInst() {
if (me == NULL) {
// ... create the object;
else { return me; }

You can see the differences between this and just a normal global variable - you can get an instance of the singleton in pretty much any scope (cSingleton::getInst()). That with the added protection of not being able to create redundant instances of the class is pretty much all the benefits I can think of to using a singleton.

Bottom line: its messy.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!