Jump to content
  • Advertisement


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


What is a Singleton Class

This topic is 5854 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.. Just a quick question. What is a Singleton Class... I just read it and i forgot where i read it. But it stuck in my head... so if someone can please explain what Singleton is actually... Also another question... I know this may sound stupid... But is there any real difference between defining a function as void Run(); void Run (void); Is there any real difference between both functions... I mean they have the difference is (void), but is there any difference on the programming side... Thanks to ever who is willing to help...

Share this post

Link to post
Share on other sites
Singleton is an object that exists in only one instance per program. For example, a texture manager. There are several articles here on GameDev concerning the singletons.

For your second question: back in the days of C, if you declared a function like so:

void Run();

you could legally pass any number of parameters to it. Empty parameter list meant "unspecified number of parameters". Therefore,

void Run(void);

told the compiler that there are no parameters. In C++, both declarations are equivalent and mean that the function accepts no parameters.

Share this post

Link to post
Share on other sites
A singleton class is a design pattern (search for "gang of four", "design patterns", or something similar for more info about these).

There must only be one of a singleton class at any time and it achieves this by hiding the constructor (e.g. making it private or protected). You have to use a static function to get a pointer or reference to the class for use elsewhere.

This alleviates a possible problem from global variables:

class CUnique

CUnique a,b; <-- oh oh!

In the above example you''ve got two different CUniques - that''s not good as we only want one. This means you need discipline. With a singleton, however, such a situation is impossible.

I think there are a couple of articles on singletons on the main page here - go check.

As for your second question: in C empty parentheses mean "any number of unknown arguments", IIRC. You''d want to use (void) instead. However, for C++ the empty parenthesis is interpreted as meaning void, so for that () and (void) are equivalent.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!