Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Singleton class with static member variables

Reply
Thread Tools

Singleton class with static member variables

 
 
LinuxGuy
Guest
Posts: n/a
 
      06-15-2005
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.

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-15-2005
On 15 Jun 2005 03:44:19 -0700, "LinuxGuy" <(E-Mail Removed)>
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
LinuxGuy
Guest
Posts: n/a
 
      06-15-2005
ya Thanks for help..

 
Reply With Quote
 
binbag@math.com
Guest
Posts: n/a
 
      06-15-2005
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

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      06-16-2005
On Wed, 15 Jun 2005 14:44:19 +0400, LinuxGuy <(E-Mail Removed)>
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
 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      06-16-2005
On Wed, 15 Jun 2005 16:06:01 +0400, <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Class variables, instance variables, singleton; Ruby v. C++ Ralph Shnelvar Ruby 29 11-30-2009 07:43 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM



Advertisments