Archived

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

karan13

What is a Singleton Class

Recommended Posts

karan13    122
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
IndirectX    122
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
Alimonster    185
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