By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Singleton class with static member variables

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 15 Jun 2005 03:44:19 -0700, "LinuxGuy" <ra**********@gmail.com>
wrote:
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.


Well, the function used to return the singleton instance (usually
called "getInstance()" or something similar, and usually returning a
pointer to the singleton instance) MUST be static -- can you figure
out why? For similar reasons, there is often a static "unload()" or
"destroy()" function.

Otherwise, I would assume that other static functions are there for
the same reason we have static functions in any non-singleton class.
One of the more important ones might be that you cannot assign the
address of a non-static member function to a regular function pointer.

--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #2

P: n/a
ya Thanks for help..

Jul 23 '05 #3

P: n/a
The normal usage of a singleton is that it is constructed using a
getInstance() function with some code like
if ( NULL == pInst )
{ Create/Initialise }

return pInst;

Of course the first time you call getInstance, the object doesn't
exist. Obviously the code "exists", but not the data.
Static functions can be called, even if an object of that class has not
been created. To protect you, it's not possible to access data members
because they may not have been set up.

However you may access static data, because well umm err it's static,
and it's life cycle is independant of the class it is referenced in.
Thus it exists typically for the life of the program.
Note that you declare class statics outside the class so they are not
much different from globals, but with more limited visibility.

DominiConnor
Quant Headhunter

Jul 23 '05 #4

P: n/a
On Wed, 15 Jun 2005 14:44:19 +0400, LinuxGuy <ra**********@gmail.com>
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.


It might be that is not a singleton rather monostate pattern, that is you
can create as many objects of it as you like and yet they all share the
same state.

--
Maxim Yegorushkin
Jul 23 '05 #5

P: n/a
On Wed, 15 Jun 2005 16:06:01 +0400, <bi****@math.com> wrote:
The normal usage of a singleton is that it is constructed using a
getInstance() function with some code like
if ( NULL == pInst )
{ Create/Initialise }

return pInst;


I don't think there exist such a thing as "normal usage of a singleton". I
can, for example, have a header file with a constant pointer guarantied to
be initialized with an address of a valid object through the lifetime of
my program, and this is also a model of singleton concept. IMO, it's a
common misconception to think that a singleton must have a static
getInstance() member function. Anything that behaves as singleton is a
singleton.

--
Maxim Yegorushkin
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.