Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

singleton question

This topic is 4770 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
Advertisement
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 {
private:
cSingleton(); // notice the constructor is hidden
// thus you cannot make new instances

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

public:
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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!