 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
LinuxGuy Guest
|
Posted: Wed Jun 15, 2005 10:40 pm Post subject: Singleton class with static member variables |
|
|
Hi,
I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Timo Geusch Guest
|
Posted: Thu Jun 16, 2005 8:23 am Post subject: Re: Singleton class with static member variables |
|
|
LinuxGuy wrote:
| Quote: | I have come across singleton class with some member variables are
declared as static with public scope.
|
That doesn't sound like a very good idea to me, but it's hard to
determine this without looking at the actual source code. What were
those variables and how did they relate to the Singleton itself?
| Quote: | As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.
|
Gissa some source code and we may be able to figure it out...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Fri Jun 17, 2005 1:34 pm Post subject: Re: Singleton class with static member variables |
|
|
LinuxGuy wrote:
| Quote: | Hi,
I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.
|
One likely reason for declaring data members of a singleton static is
to avoid tying the lifetime of those variables to the lifetime of the
singleton object itself. In fact if the singleton has no special
initialization nor termination requirements, it's often easiest to
dispense with the singleton object completely and declare all of its
data members and methods as static.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antonio Maiorano Guest
|
Posted: Mon Jun 20, 2005 9:06 am Post subject: Re: Singleton class with static member variables |
|
|
| Quote: | As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.
|
One reason this is done is to avoid having those variables created on the
heap. This can save on the expensive creation time of the singleton, and can
also improve cache coherency and alignment by having the compiler position
the memory blocks in static memory.
Antonio Maiorano
Software Engineer
Electronic Arts - Montreal
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jun 20, 2005 9:17 am Post subject: Re: Singleton class with static member variables |
|
|
Greg wrote:
| Quote: | LinuxGuy wrote:
I have come across singleton class with some member
variables are declared as static with public scope.
As singleton class always return only one
instance. ie. single copy of object is maintained all the
time. can someone tell me the reason behind declaring those
variables as static one.
One likely reason for declaring data members of a singleton
static is to avoid tying the lifetime of those variables to
the lifetime of the singleton object itself. In fact if the
singleton has no special initialization nor termination
requirements, it's often easiest to dispense with the
singleton object completely and declare all of its data
members and methods as static.
|
I'd say that typically, the arguments concerning object lifetime
work in the other direction. If someone calls
Singleton::instance(), or anything that accesses one of the
static variables, from the constructor of a static object in
another translation unit, he has no guarantee that the static
members have been constructed.
Of course, if one can be sure that the class will not be used in
constructors of static objects, it may be useful that the static
members will be constructed before the first call to
Singleton::instance(). I've also used static members at times
to ensure that the singleton object itself was constructed
before entering main (and thus, before the initialization of a
multithreaded context).
--
James Kanze GABI Software
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 |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jun 20, 2005 2:07 pm Post subject: Re: Singleton class with static member variables |
|
|
Antonio Maiorano wrote:
| Quote: | As singleton class always return only one
instance. ie. single copy of object is maintained all the
time. can someone tell me the reason behind declaring those
variables as static one.
|
| Quote: | One reason this is done is to avoid having those variables
created on the heap. This can save on the expensive creation
time of the singleton,
|
Creation time of a singleton is, almost by definition,
irrelevant, since it only occurs once.
| Quote: | and can also improve cache coherency and alignment by having
the compiler position the memory blocks in static memory.
|
That depends a lot on the implementation. The linkers I know
don't do anything to ensure cache alignment. Nor does operator
new, but there are generally options with malloc to do so.
--
James Kanze GABI Software
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
|
|