 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun Jun 04, 2006 5:25 am Post subject: Singleton Pattern without Static Method |
|
|
Hi Guys
I have been trying to understand if there's a way to implement
Singleton Pattern without Using Static Methods?
Thanks for the help in advance.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sun Jun 04, 2006 6:18 pm Post subject: Re: Singleton Pattern without Static Method |
|
|
Venkata.Nanduri (AT) gmail (DOT) com wrote:
| Quote: | 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.
--
Valentin Samko - http://www.valentinsamko.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Tue Jun 06, 2006 4:21 am Post subject: Re: Singleton Pattern without Static Method |
|
|
Valentin Samko wrote:
| Quote: | Venkata.Nanduri (AT) gmail (DOT) 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 (AT) neuf (DOT) 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! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|