Singleton class in C++

Discussion in 'C++' started by bob smith, Aug 20, 2013.

  1. bob smith

    bob smith Guest

    If someone asks you to write a Singleton class in C++, can someone help me see what the answer would be?

    In particular, I don't know if the getInstance() function ought to return Singleton* or Singleton&.

    I'm also concerned about when and how the memory for this class will be deallocated.

    Thanks.
     
    bob smith, Aug 20, 2013
    #1
    1. Advertising

  2. bob smith

    Ian Collins Guest

    bob smith wrote:

    {please clean up the mess google will inevitably make of your replies!}

    > If someone asks you to write a Singleton class in C++, can someone
    > help me see what the answer would be?


    That depends on the exact requirements. Something as simple as

    template <typename T>
    struct Singleton
    {
    static T& getInstance()
    {
    static T instance;

    return instance;
    }
    };

    is often good enough.

    > In particular, I don't know if the getInstance() function ought to
    > return Singleton* or Singleton&.


    As above.

    > I'm also concerned about when and how the memory for this class will
    > be deallocated.


    In the example I cited, the instance will be constructed the first time
    getInstance() is called and destroyed when the programme exits.

    --
    Ian Collins
     
    Ian Collins, Aug 20, 2013
    #2
    1. Advertising

  3. bob smith wrote:

    >If someone asks you to write a Singleton class in C++ ...


    An alternative to Ian Collins approach, that I find useful, is a
    "Monostate" class. (1)
    That is, a class were all data members are static, so every instance
    shares a common state.

    >In particular, I don't know if the getInstance() function ...


    Not getInstance() method is required. Instances of such classes can be
    declared at will without allocating additional storage (beyond minimal
    size and alignment requirements + the rule that every object should
    have a unique address.)

    >I'm also concerned about when and how the memory for this class will be deallocated.

    As in Ian's example, memory will be released when the program exits.

    (1) From Stephen Dewhurst's book "C++ Common Knowledge"
    See also http://c2.com/cgi/wiki?MonostatePattern
    --
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
     
    Roberto Waltman, Aug 20, 2013
    #3
  4. bob smith

    Luca Risolia Guest

    Paavo Helde wrote:
    > Sometimes it
    > is simpler to allocate singleton objects dynamically and never destroy
    > them. This leaves the memory cleanup to the OS on the process shutdown.
    > However, this practice may be formally considered as a memory leak (for
    > some useless definition of "memory leak") and thus frowned upon by some
    > people.


    In those cases, the instance can be wrapped with a static unique_ptr to avoid
    resource leaking.
     
    Luca Risolia, Aug 20, 2013
    #4
  5. On 08/20/2013 01:19 PM, Luca Risolia wrote:
    > Paavo Helde wrote:
    >> Sometimes it
    >> is simpler to allocate singleton objects dynamically and never destroy
    >> them. This leaves the memory cleanup to the OS on the process shutdown.
    >> However, this practice may be formally considered as a memory leak (for
    >> some useless definition of "memory leak") and thus frowned upon by some
    >> people.

    >
    > In those cases, the instance can be wrapped with a static unique_ptr to avoid
    > resource leaking.
    >

    Andrei Alexandrescu's "Modern C++ Design" has Chapter 6 on Singletons,
    with explicit code. Covers design issues and approaches.
     
    Norman J. Goldstein, Aug 20, 2013
    #5
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,285
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    172
  3. Trans
    Replies:
    12
    Views:
    284
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    210
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

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

Share This Page