#define __CRT_SECURE_NO_WARNINGS
#include<string>
#include<vector>
#include<iostream>
#include<vld.h>
//using namespace std;
/*
@作者:莫忘输赢
@时间:
2020/02/22 22:09
@版本:v1
@单例模式
@作用:
提供全局单一访问对象
*/
class Singleton
{
private:
Singleton(){ }
static Singleton* single;
public:
static Singleton* GetInstance()
{
if (single == NULL)
{
single = new Singleton();
}
return single;
}
static void FreeInstance()
{
if (single != NULL)
{
delete single;
single = NULL;
}
}
};
Singleton* Singleton::single = NULL;//静态变量初始化
int main()
{
Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
if (s1 == s2)
{
std::cout << "相等"<<std::endl;
}
else
{
std::cout << "不相等" << std::endl;
}
//delete s2;
//delete s1;
Singleton::FreeInstance();
return 0;
}
设计模式总篇-单例模式
猜你喜欢
转载自blog.csdn.net/wjl18270365476/article/details/104451767
今日推荐
周排行