Implement Singleton Using or Not Using dynamic object.

S

Steven Woody

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;
}
---------------------------------------
 
A

Axter

Steven said:
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;
}
 
J

Jay Nabonne

hi,

my friend ask me the following question. even thogth i prefer the
solution 3, but i owe an explanation.
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top