My opinion: There might be some cases where a singleton is the right thing, though I lack experience there as I've never needed one ...
Quote:Original post by davepermen
but sometimes they fit your needs. question is, when do singletons fit your need and globals don't? singletons are much more limiting, when do you gain from it?
... but I don't like blanket statements (admittedly sometimes I make them unintentionally). Dave, the last time I used a global variable was somewhen around 2001. I began programming around 1999, afair. Maybe you can enlighten us with a usage scenario where you really need a unit- or program-scope mutable variable, before further requesting examples for correct use of singletons?
Quote:i'm all for limiting stuff in your code, that's why you shouldn't use globals.
Just to clarify: I guess you basically mean that it's a good thing to tighten the scope or lifetime of an object as much as possible? (just curious, that phrase of yours can be interpreted in many ways)
Quote:but i never limit classes, i always limit objects.
In which respect exactly do you limit it all?
Quote:limiting functionality is just bad.
I limit functionality all the time, especially when writing const correct code. Also, I am going to limit functionality to enable YAGNI principles (e.g. I will make the standard constructor, the standard copy constructor, and the copy assignment operator private by default, and introduce more freedom only when needed; you won't believe how many times that saved some buttes). Often, you will also see me making a class final, so no one can derive from it.