C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Singleton Pattern without Static Method

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sun Jun 04, 2006 5:25 am    Post subject: Singleton Pattern without Static Method Reply with quote



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





PostPosted: Sun Jun 04, 2006 6:18 pm    Post subject: Re: Singleton Pattern without Static Method Reply with quote



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





PostPosted: Tue Jun 06, 2006 4:21 am    Post subject: Re: Singleton Pattern without Static Method Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.