Implement Singleton Using or Not Using dynamic object.

Discussion in 'C++' started by Steven Woody, Dec 29, 2005.

  1. Steven Woody

    Steven Woody Guest

    hi,

    my friend ask me the following question. even thogth i prefer the
    solution 3, but i owe an explanation.


    -------------------------------

    I create three versions for singletone pattern below, plz compare them
    with advantage and disadvantage:

    1.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp& getInstance() { return appInstance; }

    private:
    MyApp() {;}

    private:
    static MyApp appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInstance;

    2.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp* getInstance() { return &appInstance; }

    private:
    MyApp() {;}

    private:
    static MyApp appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInstance;
    3.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp* getInstance();

    private:
    MyApp() {;}

    private:
    static MyApp* appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInstance==NULL;

    MyApp::MyApp()
    {
    }

    MyApp* MyApp::getInstance()
    {
    if (appInstance == NULL)
    appInstance = new MyApp();

    return appInstance;
    }
    ---------------------------------------
     
    Steven Woody, Dec 29, 2005
    #1
    1. Advertising

  2. Steven Woody

    Axter Guest

    Steven Woody wrote:
    > hi,
    >
    > my friend ask me the following question. even thogth i prefer the
    > solution 3, but i owe an explanation.
    >
    >
    > -------------------------------
    >
    > I create three versions for singletone pattern below, plz compare them
    > with advantage and disadvantage:
    >
    > 1.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp& getInstance() { return appInstance; }
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInstance;
    >
    > 2.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp* getInstance() { return &appInstance; }
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInstance;
    > 3.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp* getInstance();
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp* appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInstance==NULL;
    >
    > MyApp::MyApp()
    > {
    > }
    >
    > MyApp* MyApp::getInstance()
    > {
    > if (appInstance == NULL)
    > appInstance = new MyApp();
    >
    > return appInstance;
    > }
    > ---------------------------------------


    If you're singleton needs to be access from multiple translation units
    before calling main(), then I recommend a reference return type of
    option 3.
    It's safer to return a reference then to return a pointer, because it's
    easier to accidently delete a return pointer.

    //MyApp.hpp
    class MyApp
    {
    public:
    static MyApp& getInstance();
    private:
    MyApp() {}
    static MyApp* appInstance;
    };

    //MyApp.cpp
    MyApp MyApp::appInstance==NULL;

    MyApp& MyApp::getInstance()
    {
    if (appInstance == NULL)
    appInstance = new MyApp();

    return *appInstance;
    }
     
    Axter, Dec 29, 2005
    #2
    1. Advertising

  3. Steven Woody

    Jay Nabonne Guest

    On Wed, 28 Dec 2005 17:41:46 -0800, Steven Woody wrote:

    > hi,
    >
    > my friend ask me the following question. even thogth i prefer the
    > solution 3, but i owe an explanation.
    >
    >
    > -------------------------------
    >
    > I create three versions for singletone pattern below, plz compare them
    > with advantage and disadvantage:
    >

    <snip>

    4.

    MyApp.hpp

    class MyApp
    {
    public:
    static MyApp& getInstance()
    {
    static MyApp appInstance;
    return appInstance;
    }

    private:
    MyApp() {;}
    };

    Doesn't get constructed until you call it the first time (so you don't
    have to worry about initialization order), and it will be destructed
    automatically when the program exits.

    - Jay
     
    Jay Nabonne, Dec 29, 2005
    #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. Steve Jasper
    Replies:
    5
    Views:
    1,323
    Steve Jasper
    Nov 20, 2003
  2. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,255
    Proton Projects - Moin
    Mar 27, 2007
  3. Wilhelm
    Replies:
    1
    Views:
    167
  4. Trans
    Replies:
    12
    Views:
    280
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    206
    David A. Black
    Jun 9, 2008
Loading...

Share This Page