Ai
1 Star 0 Fork 0

rabbitqyh/DesignPattern

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SingletonPattern.cpp 747 Bytes
一键复制 编辑 原始数据 按行查看 历史
yaocoder 提交于 2013-06-09 13:49 +08:00 . singleton
/*
单例模式:一个类只能拥有一个实例
*/
#include <iostream>
#include <string>
using namespace std;
class Singleton
{
public:
static Singleton* GetInstance()
{
if (NULL == singleton_)
{
singleton_ = new Singleton();
}
return singleton_;
}
private:
static Singleton* singleton_;
};
Singleton* Singleton::singleton_ = NULL;
class SingletonMultiThread
{
public:
static SingletonMultiThread* GetInstance()
{
if (NULL == singleton_)
{
/*TODO:同步*/
if (NULL == singleton_)
{
singleton_ = new SingletonMultiThread();
}
}
return singleton_;
}
private:
static SingletonMultiThread* singleton_;
};
SingletonMultiThread* SingletonMultiThread::singleton_ = NULL;
int main()
{
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rabbitqyh/DesignPattern.git
git@gitee.com:rabbitqyh/DesignPattern.git
rabbitqyh
DesignPattern
DesignPattern
master

搜索帮助