Singleton class with static member variables

Discussion in 'C++' started by LinuxGuy, Jun 15, 2005.

  1. LinuxGuy

    LinuxGuy Guest

    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.
     
    LinuxGuy, Jun 15, 2005
    #1
    1. Advertising

  2. On 15 Jun 2005 03:44:19 -0700, "LinuxGuy" <>
    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
     
    Bob Hairgrove, Jun 15, 2005
    #2
    1. Advertising

  3. LinuxGuy

    LinuxGuy Guest

    ya Thanks for help..
     
    LinuxGuy, Jun 15, 2005
    #3
  4. LinuxGuy

    Guest

    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
     
    , Jun 15, 2005
    #4
  5. On Wed, 15 Jun 2005 14:44:19 +0400, 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.


    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
     
    Maxim Yegorushkin, Jun 16, 2005
    #5
  6. On Wed, 15 Jun 2005 16:06:01 +0400, <> 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
     
    Maxim Yegorushkin, Jun 16, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran
    Replies:
    4
    Views:
    842
    Micah Cowan
    Jan 12, 2005
  2. dolphin
    Replies:
    3
    Views:
    1,404
    Pete Becker
    Dec 5, 2007
  3. Paul McMahon
    Replies:
    3
    Views:
    224
    David A. Black
    Jun 9, 2008
  4. Ralph Shnelvar
    Replies:
    29
    Views:
    833
    David Masover
    Nov 30, 2009
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    222
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page