#include <iostream>
using namespace std;
class Singleton
{
private:
Singleton();
public:
static Singleton *GetInstance();
virtual ~Singleton();
public:
static Singleton *instance;
};
Singleton *Singleton::instance = NULL;
Singleton ::Singleton()
{
}
Singleton *Singleton::GetInstance()
{
if(instance == NULL)
{
instance = new Singleton();
}
return instance;
}
Singleton::~Singleton()
{
}
int main(int argc, char* argv[])
{
Singleton *test = Singleton::GetInstance();
Singleton *test1 = Singleton::GetInstance();
if(test == test1)
{
cout<< "the same instance!" << endl;
}
delete test;
test = NULL;
return 0;
}
No comments:
Post a Comment