Where to release the object in Singleton class?

Discussion in 'C' started by BS Dahiya, Sep 18, 2008.

  1. BS Dahiya

    BS Dahiya New Member

    Joined:
    Sep 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Where to release the object in Singleton class? Should we go destructor(Private) or any other static member function? I mean , what is the best solution?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Not the destructor, because deleting the object calls the destructor and that'll be recursive.
    Normally you would implement a get method which creates the object if it doesn't exist, then returns a reference to itself, and a release method which deletes the object if there are no references left. These are both static functions. If it's a global object of which there will always be one instance then get just returns a reference and release doesn't need to exist.
     
  3. BS Dahiya

    BS Dahiya New Member

    Joined:
    Sep 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks a lot. It is really fruitful.I got the idea,
    Regards,
    BSD
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice