Singleton class with static member variables

L

LinuxGuy

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.
 
B

Bob Hairgrove

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.
 
B

binbag

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
 
M

Maxim Yegorushkin

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.
 
M

Maxim Yegorushkin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top