Share this post

Link to post

Share on other sites

You should probably avoid using compiler macros altogether (just use inline functions which can provide the same speed, but with the bonus of type-safety). I would definitely steer clear of the usage you show there... is it really that much of an effort to type the class name?

If you need to make a number of calls to the object within a function, just declare a nice short-named pointer to the singleton class and use that instead of the fully-qualified name each time...

i.e.

MySingleton* handle;

handle = MySingleton::Get();

...

Much better at conveying the meaning of the code... I mean come on man , that macro if just frikkin'' laziness in the extreme

Share this post

Link to post

Share on other sites

quote:Original post by Bad Monkey You should probably avoid using compiler macros altogether (just use inline functions which can provide the same speed, but with the bonus of type-safety).

Just to be clear, there''s no issue of type safety in this instance. It''s mostly about clarity here. That being said...

quote:I would definitely steer clear of the usage you show there... is it really that much of an effort to type the class name?

I completely agree. If you made the macro (or inline function name) clear enough, you''d basically be typing the much feared "MySingleton::get()".

quote:If you need to make a number of calls to the object within a function, just declare a nice short-named pointer to the singleton class and use that instead of the fully-qualified name each time...

i.e.

MySingleton* handle;

handle = MySingleton::Get();

...

Much better at conveying the meaning of the code... I mean come on man , that macro if just frikkin'' laziness in the extreme

I agree, though I''m not sure I''d even make the variable (that is, assuming Get() is fast). Of course, I like long, descriptive names. I''d rather spend 3 more seconds typing now than spend an hour down the road trying to figure out what the heck is going on. (comments would also work, but naming the variables properly can lessen the need for comments and force you to "maintain" your "comments")

Share this post

Link to post

Share on other sites

quote:Original post by Anonymous Poster Think of a design that doesn't require singletons and you'll be better off.

How so? Sometimes a Singleton can be the appropriate tool for the job.

I don't want to have to store a reference to the Engine object in every freakin other class I make that needs to access it. And then write appropriate constructors to handle the Engine object, and then make sure I pass the Engine object to every other class that I make an instance of that needs access to the Engine. That's just nuts.

Also, the singleton pattern enforces the design intent on the programmer. When a class is declared singleton, and another programmer comes in and tries to instantiate that class, the compiler ( or run-time, in the example I gave above, but better if it's the compiler ) will tell the programmer that the class is intended to be used as a singleton, and as such only one instance is allowed.

[edited by - daerid on May 26, 2003 10:17:14 PM]

0

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

quote:Original post by daerid I don''t want to have to store a reference to the Engine object in every freakin other class I make that needs to access it. And then write appropriate constructors to handle the Engine object, and then make sure I pass the Engine object to every other class that I make an instance of that needs access to the Engine. That''s just nuts.

I haven''t found it a burden at all. You just need to think the design in such a way that Engine won''t be needed in many places. But say, you realize it''d be useful to run several Engines within a single program when you extend the game to have multiplayer capabilities and need several Engines to run several game arenas. What''re you going to do then?

quote: When a class is declared singleton, and another programmer comes in and tries to instantiate that class, the compiler ( or run-time, in the example I gave above, but better if it''s the compiler ) will tell the programmer that the class is intended to be used as a singleton, and as such only one instance is allowed.

Oh, please. Why would someone try to make a new Engine abruptly? If someone wants to make a new Engine-object, she probably has a good reason to do so, like the multiplayer system mentioned above. I can''t think of a single thing that should be singular; You can always have a parallel world.

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

Yes, sometimes I end up being pretty fanatic against singletons even though I have to admit that often the solution that gets the job done quickest is the most beneficial. And that may mean using singletons. Yet, IMHO, code without singletons or other globals would be best code (easiest to extend and understand), but if it takes a lot longer to make it work so, it just may not be worth it.

Share this post

Link to post

Share on other sites

quote:daerid: I don''t want to have to store a reference to the Engine object in every freakin other class I make that needs to access it.

The problem is once you''ve made the engine a singleton you''ll just let every and any class access it (because they can). You would have a better design if you didn''t use a singleton and actually had to tackle thinking about dependencies, reusability etc.

If you think the only alternative to a singleton is storing a reference to the class in every class that needs to know about it then you have a lot to learn.

I always have Initialise and Uninitialize functions aswel in my manager classes, the only thing the coder needs to do is make sure the delete function is called for every Manager class to ensure that the memory is freed. (I put it at the end of its own Uninitialize function)

0

Share this post

Link to post

Share on other sites

Of course I do have some rules when using the such as that the class mush be self-sustained meaning that if it needs a variable to work, say HINSTANCE, then I wont be asking for it from another singleton but I'd have it sent as a parameter in the initialize function.