Singleton.h
class A {
private :
static A * _instance;
protected :
A();
public :
static A * getInstance();
void sayhello();
};
Singleton.cpp
#include < iostream >
#include " singleton.h "
using namespace std;
A * A::_instance = 0 ;
A::A() {}
A * A::getInstance() {
if (_instance == 0 )
_instance = new A;
return _instance;
}
void A::sayhello() {
cout << " Hello! " << endl;
}
testsingleton.cpp
#include " singleton.h "
#include < iostream >
using namespace std;
int main()
{
A * abc = A::getInstance();
abc -> sayhello();
return 0 ;
}
class A {
private :
static A * _instance;
protected :
A();
public :
static A * getInstance();
void sayhello();
};
Singleton.cpp
#include < iostream >
#include " singleton.h "
using namespace std;
A * A::_instance = 0 ;
A::A() {}
A * A::getInstance() {
if (_instance == 0 )
_instance = new A;
return _instance;
}
void A::sayhello() {
cout << " Hello! " << endl;
}
testsingleton.cpp
#include " singleton.h "
#include < iostream >
using namespace std;
int main()
{
A * abc = A::getInstance();
abc -> sayhello();
return 0 ;
}