By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Implement Singleton Using or Not Using dynamic object.

P: n/a
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;
}
---------------------------------------

Dec 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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;
}

Dec 29 '05 #2

P: n/a
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

Dec 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.