quote:But i just realised i may need more than one of the same class to store varying data. Is there a way to do this while still being able to call the class from anywhere and use it anywhere?
You don't want a singleton then. Singletons enforce a single instance of a class that is accessible anywhere.
I don't really see the point of a simple singleton class, either, it is just a class with ctor/dtor private and a method called instance() that returns a reference. Loki's Singleton wrapper is meaningful because he at least has templated policy classes that can make the singleton thread-safe and other cool things. The author of that is brilliant.
(singleton tirade)
Resist the urge to use globals/singletons until you have no other options. Your back should be to the wall (proverbially) and it should be very clear that you need a single instance, plus global accessibility (e.g. clients shouldn't have to concern themselves with providing the information for elegance).
Singletons do rock in that a Meyers singleton won't be instantiated unless it is actually needed. To get around lifetime issues, I use a LifetimeManager that manages the dependencies, I recommend people do the same if its possible for singletons to be dependent on one another.
Just because a singleton is a design pattern doesn't mean it should be everywhere. Just because you need only one instance of a class doesn't mean it should be a singleton. I see in a bunch of articles where people make stuff singletons, and have to ask why. Fighting with initialization/destruction order sucks. Enforcing singletons in an application spread across DLLs forces you to fight either DLL memory boundaries or be dependent on the runtime library being linked in at runtime, and therefore present on the user's system.
Singletons seem like a hack to support something that C++ has trouble expressing 'natively.' I cannot think of a better way to do it than we have right now. Issues with separate heaps/runtime libraries make things far more difficult than they need to be.
[edited by - antareus on November 9, 2003 2:55:36 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis