Jump to content
  • Advertisement

Archived

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

Singleton classes?

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

What''s the advantage of using a class with a single static instance over using a namespace or simply *gasp* globals? The class approach looks a bit less efficient, but no more functional. "Eagles may soar, but weasels don''t get sucked into jet engines."

Share this post


Link to post
Share on other sites
Advertisement

You know exactly when it is createdm, so it is no platform dependant...

It is nicer and more extensible...(My opinion..)



What the hells!

Share this post


Link to post
Share on other sites
Back in my C days I would use a different name space or even start function names with the name of the libarary i.e. GFXMove, GFXLine, etc...

But now I use C++, so when in Rome.....



D.V.

Share this post


Link to post
Share on other sites
Classes are no less efficient than using a global structure. They''re slightly less efficient than using global data.

A namespace is comparable to a singleton class, but one would assume that a singleton class is a small-ish set of interfaces; namespaces are more for the purpose of encapsulating entire libraries.

Globals are outright evil since they pollute the global namespace, and make integration into other projects very painful.

Share this post


Link to post
Share on other sites
It honestly all depends on what your doing. For example if you want to have the bennifits of a ''Global'' object, but don''t to actually implument such a poor practice a singleton is the way to go. However their are plenty of C alternatives. The best place for singletons are in things like your manager classes or console classes... things that need to be everwhere at once, but have no owners. Hope that will clarify anything.

CodeSmith the Pixel Pusher
www.cs.trinity.edu/~csmith8

Share this post


Link to post
Share on other sites
Well, a namespace doesn''t have a constructor, I would think, so you''d initialize it explicitly, so you do in fact know when it''s ''created''.

Also, doesn''t a namespace simply cease to exist in the executable, rendering its members global, for all intents and purposes?

Moreover, every time you use a singleton class, you''ve got that extra level of indirection. (er.. yeah... major speed hit there, I''m sure :D)

Stoffel''s comment about conventions makes some sense, but calling a static member function looks a heck of a lot like calling a function from a namespace, and both look nicer than thingiecontroller.GetInstance()->ThingieMethod();

Anyhow, thanks all!

"Eagles may soar, but weasels don''t get sucked into jet engines."

Share this post


Link to post
Share on other sites
I think a good point for using singleton is this. Say you use global structures for half of you developement, then you get to a point and find that you really want two of that structure, but you''ve only got one! Singleton can be quickly tweaked back to a normal class & you''ve got multiple instances, not so easy to do with global variables. Also from a pure point of view, if you''re going to use OO, you should embrace it''s philosophy & not "dirty" it with global variables (which violates encapsulation in a bad way).

Just my $0.02

Brad

Share this post


Link to post
Share on other sites
namespaces are used to avoid name collisions between different codebases

they''re a completely different concept than singletons, you don''t have any of the functionality you get with classes:

- inheritance, polymorphism (eg. Application singleton)
- constructors/destructors (initialization, cleanup)
- encapsulation (you could use static/file-scope globals however)

about the speed hit:

- inline small speed-critical methods
- passing a this-pointer for a large calculation-intensive method isn''t going to effect performance

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!