Archived

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

Promit

What advantages does a Singleton have over a static class?

Recommended Posts

Promit    13246
So far, although I''ve seen a couple Singleton patterns, I have yet to see one where it seemed that it would be more effective than simply making all the members of the class static. By using static, you avoid the horrendous CClass::This()->SomeMethod(Params) and just say CClass::SomeMethod(Params). I haven''t seen any clean syntaxes for singletons. Am I missing something here?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
you need to explicitly init a static class ; can''t think of anything else though.

Share this post


Link to post
Share on other sites
Promit    13246
Isn''t a static class automatically constructed before your program entry point is called?

Or is that only static members?

Share this post


Link to post
Share on other sites
SiCrane    11839
No, the C++ standard specifically states that it's implementation defined whether any object with static storage duration has been dynamically initialized before the entry point is called. In practice, most compilers will dynamically initialize objects at the namespace level before the entry point is called, but there's no guarantees. Moreover, the order of initializations are undefined so if one object with static storage duration needs to access another as part of initialization, then Bad Mojo can occur.

edit: bad grammar

[edited by - SiCrane on January 17, 2004 12:55:35 PM]

Share this post


Link to post
Share on other sites
Sneftel    1788
singletons are more flexible. Their initialization, destruction, and possible re-initialization semantics may be exactly specified by the programmer. In contrast, if you make all class member functions static, all class members will also have to be static, so their initialization and destruction semantics are only what the C++ standard defines them to be.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Promit    13246
quote:
Original post by Magmai Kai Holmlor
quote:
Original post by Promit
So far, although I''ve seen a couple Singleton patterns



You''ve seen a couple of singleton implementations, there''s only one singleton pattern.


Eh, right, just used the wrong word


psykr: I dunno. I guess it comes to the same thing.


So is initialization order really the only problem with static things, namespace things, etc.?

Share this post


Link to post
Share on other sites