#include <iostream>
using namespace std;
//懒汉
class Singleton
{
public:
static Singleton *getInstance()
{
//Lock
if (pInstance == NULL)
{
return new Singleton();
}
//UnLook
}
private:
static Singleton *pInstance;
Singleton(){}
};
//饿汉
//无论是否调用该类的实例,在程序开始时就会产生该类的实例,并在以后就返回该类的实例
class AnotherSingleton
{
public:
static AnotherSingleton *pInstance;
static AnotherSingleton *getInstance()
{
return pInstance;
}
private:
AnotherSingleton()
{
}
};
AnotherSingleton * AnotherSingleton::pInstance = new AnotherSingleton();
int main(int argc,char *argv[])
{
AnotherSingleton*p = AnotherSingleton::getInstance();
return 0;
}