Re: Singleton Pattern without Static Method
Valentin Samko wrote:
Venkata.Nanduri@gmail.com wrote:
I have been trying to understand if there's a way to implement
Singleton Pattern without Using Static Methods?
= myfile.h =
class Singleton { ... };
Singleton* getInstance();
= myfile.cpp =
Singleton s;
Singleton* getInstance() { return &s; }
Of course one can not safely use this before "main", but one
can use a nifty counter or other techniques to address this
issue.
It will also cause problems as soon as you use the idiom for
more than one class -- you need some sort of naming convention.
Also, of course, it's not a singleton. There's nothing to
prevent a user from declaring a local instance.
In answer to his question, you can always use a friend function
instead of a static member. But why bother? Or more to the
point, what's the real difference?
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]