Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Implement Singleton Using or Not Using dynamic object.

Reply
Thread Tools

Implement Singleton Using or Not Using dynamic object.

 
 
Steven Woody
Guest
Posts: n/a
 
      12-29-2005
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;
}
---------------------------------------

 
Reply With Quote
 
 
 
 
Axter
Guest
Posts: n/a
 
      12-29-2005
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;
}

 
Reply With Quote
 
 
 
 
Jay Nabonne
Guest
Posts: n/a
 
      12-29-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM
Can you implement a Singleton within J2ME Apps, without using ThreadLocal? Steve Jasper Java 5 11-20-2003 06:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57