c++设计模式--单件模式
2017-05-28 11:59
381 查看
--前言:代码,代码,你可知我写你不容易啊。本来想说几句的,增加可读性。但是后面再来改吧!
#include<iostream>
#include<string>
using namespace std;
class Singlton {
private:
string name;
Singlton() {}
// 静态成员,指向唯一的对象
static Singlton * _instance;
public:
// get _instance;
static Singlton * Instance() {
if (_instance == NULL)
_instance = new Singlton;
return _instance;
}
void setName(string str) { name = str; }
string getName() { return name; }
};
// 初始化:类型 变量名 = 初始值;
Singlton * Singlton::_instance = NULL;
void fun() {
Singlton * s = Singlton::Instance();
s->setName("张三");
}
int main()
{
Singlton * t = Singlton::Instance();
t->setName("李四");
fun();
cout << t->getName() << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Singlton {
private:
string name;
Singlton() {}
// 静态成员,指向唯一的对象
static Singlton * _instance;
public:
// get _instance;
static Singlton * Instance() {
if (_instance == NULL)
_instance = new Singlton;
return _instance;
}
void setName(string str) { name = str; }
string getName() { return name; }
};
// 初始化:类型 变量名 = 初始值;
Singlton * Singlton::_instance = NULL;
void fun() {
Singlton * s = Singlton::Instance();
s->setName("张三");
}
int main()
{
Singlton * t = Singlton::Instance();
t->setName("李四");
fun();
cout << t->getName() << endl;
system("pause");
return 0;
}
相关文章推荐
- 设计模式--单件实现C++
- c++ 设计模式之单件模式
- c++ 设计模式之单件模式
- 也谈单件模式类的设计(C++)
- C++设计模式-单件
- c++设计模式:单件模式(Singleton Pattern)
- c++ 设计模式之单件模式
- 设计模式 - 单件模式 C++
- 设计模式----单件模式(c++)
- [设计模式] C++设计模式-单件 Good!!!
- 【设计模式】单件模式 c#与c++实现
- C++设计模式-单件
- 设计模式---单件(C++版) 一个简单实例(避免用户忘记delete单件实例)
- C++设计模式:单件模式
- c++设计模式:单件模式(Singleton Pattern)
- c++设计模式:单件模式(Singleton Pattern)
- c++设计模式之单件模式
- C++单件类,设计模式-单件
- Head First 设计模式 (五) 单件模式(Singleton pattern) C++实现
- Head First 设计模式 (五) 单件模式(Singleton pattern) C++实现