Alternative was to implement a Singleton Design Pattern

Discussion in 'C++' started by Daniel Kay, Sep 3, 2006.

  1. Daniel Kay

    Daniel Kay Guest

    Hello,

    currently I am reading the book "Effective C++ Third Edition" from Scott
    Meyers. While Reading Item 4 (Make sure that objects are initialized
    before they're used) I got an idea how to improve the way to implement
    the Singlton Design Pattern.

    This is the way I used to do it:

    Singleton.h
    ----------

    class Singleton {
    public:
    static Singleton* getInstance() {
    if (!m_instance) {
    m_instance = new Singleton();
    }

    return m_instance;
    }

    private:
    Singleton() { }
    ~Singleton() { }
    static Singleton* m_instance;
    }

    Singleton.cpp
    -------------

    Singleton* Singleton::m_instance = 0;




    This is the way I am planning to do it in future:

    Singleton.h
    -----------

    class Singleton {
    public:
    static Singleton* getInstance() {
    static Singleton instance;
    return &instance;
    }

    private:
    Singleton() { }
    ~Singleton() { }
    };

    (Source is only showing the concept. I didn't compile it.)


    I see the following pros:
    - No potentional memory leak when process terminates.
    - Less source code required.
    - Not necessarely a .cpp file required for the static member.

    Are there any cons I am missing?

    Thanks for your thoughts,
    Daniel Kay
     
    Daniel Kay, Sep 3, 2006
    #1
    1. Advertising

  2. Daniel Kay

    red floyd Guest

    Daniel Kay wrote:
    > This is the way I am planning to do it in future:
    >
    > Singleton.h
    > -----------
    >
    > class Singleton {
    > public:
    > static Singleton* getInstance() {
    > static Singleton instance;
    > return &instance;
    > }
    >
    > private:
    > Singleton() { }
    > ~Singleton() { }
    > };
    >
    > (Source is only showing the concept. I didn't compile it.)
    >
    >
    > I see the following pros:
    > - No potentional memory leak when process terminates.
    > - Less source code required.
    > - Not necessarely a .cpp file required for the static member.
    >
    > Are there any cons I am missing?


    See Alexandrescu's "Modern C++ Design". It discusses singletons in more
    detail than you'd ever want to know, and in particular, he discusses
    this idiom (he calls it a "Meyers Singleton", since Meyers is the
    earliest cite he found it in, I guess).
     
    red floyd, Sep 3, 2006
    #2
    1. Advertising

  3. Daniel Kay

    dasjotre Guest

    Daniel Kay wrote:
    > I see the following pros:
    > - No potentional memory leak when process terminates.
    > - Less source code required.
    > - Not necessarely a .cpp file required for the static member.


    singleton would prefferably be implemented as a template
    so this is not realy a benefit

    >
    > Are there any cons I am missing?


    I don't think there is a single implementation that satisfies
    all possible requirements.

    few cons might arrise

    - No control over singleton's liffetime, can't delete it
    see http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt
    <no link intentional>
    - Singleton must have default constructor
    - if Singleton's constructor throws you might not be able to recover
    - I can do
    delete Singleton::getInstance()
    invoking undefined behaviour, bether return reference
    instead of a pointer

    otherwise it's fine :)
     
    dasjotre, Sep 6, 2006
    #3
    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. Venkat Sadasivam
    Replies:
    6
    Views:
    427
    thufir
    Apr 16, 2008
  2. Pallav singh
    Replies:
    0
    Views:
    375
    Pallav singh
    Jan 22, 2012
  3. Pallav singh
    Replies:
    0
    Views:
    411
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    1
    Views:
    460
    Peter Remmers
    Jan 22, 2012
  5. Wilhelm
    Replies:
    1
    Views:
    172
Loading...

Share This Page