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