/* 单例模式 */
class Singleton
{
private:
static Singleton *s; /* private static */
/* 构造函数声明为private */
Singleton()
{
}
public:
static Singleton* getInstance() /* 静态函数 */
{
if (NULL == s)
{
s = new Singleton;
}
return s;
}
};
Singleton* Singleton::s = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
Singleton *s1 = Singleton::getInstance();
cout<<s1<<endl;
Singleton *s2 = Singleton::getInstance();
cout<<s2<<endl;
cout<<(s1==s2) <<endl; //1
return 0;
}
单例模式
最新推荐文章于 2025-01-06 18:04:26 发布