当前位置:网站首页>Common creation and usage of singletons
Common creation and usage of singletons
2022-06-26 13:04:00 【Lao Zhao's blog】
- Purpose : Create a global single instance , Call a class through a single object
- establish
- Definition static Member variables
- Constructor privatization
- Prevent multiple threads from using locks
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_Instance == NULL )
{
Lock(); // C++ There is no direct Lock operation , Please use the Lock, such as Boost, This is just to illustrate
if (m_Instance == NULL )
{
m_Instance = new Singleton ();
}
UnLock(); // C++ There is no direct Lock operation , Please use the Lock, such as Boost, This is just to illustrate
}
return m_Instance;
}
static void DestoryInstance()
{
if (m_Instance != NULL )
{
delete m_Instance;
m_Instance = NULL ;
}
}
int GetTest()
{
return m_Test;
}
private:
Singleton(){ m_Test = 0; }
static Singleton *m_Instance;
int m_Test;
};
Singleton *Singleton ::m_Instance = NULL;
int main(int argc , char *argv [])
{
Singleton *singletonObj = Singleton ::GetInstance();
cout<<singletonObj->GetTest()<<endl;
Singleton ::DestoryInstance();
return 0;
}边栏推荐
- P2393 yyy loves Maths II
- 倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
- Angle de calcul POSTGIS
- Go 结构体方法
- Xiaobai lazy special-win10-win11 one click installation version
- National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
- [BSidesCF 2019]Kookie 1
- Electron official docs series: Get Started
- Word文档导出(使用固定模板)
- HDU1724[辛普森公式求积分]Ellipse
猜你喜欢

C# const详解:C#常量的定义和使用

Openlayers drawing dynamic migration lines and curves
![HDU1724[辛普森公式求积分]Ellipse](/img/57/fb5098e150b5f3d91a5d0983a336ee.png)
HDU1724[辛普森公式求积分]Ellipse

四类线性相位 FIR滤波器设计 —— MATLAB源码全集

This function has none of deterministic, no SQL solution

Photoshop 2022 23.4.1增加了哪些功能?有知道的吗

倍福通过CTU和TON实现时间片大小和数量的控制

Deep parsing MySQL binlog

Explain C language 11 in detail (C language series)

Word文档导出(使用固定模板)
随机推荐
ES6:迭代器
Learning Processing Zoog
find及du -sh显示权限不够的解决方法
Splunk iowait 报警的解决
Stream流学习记录
倍福TwinCAT3实现CSV、TXT文件读写操作
Deeply analyze the differences between dangbei box B3, Tencent Aurora 5S and Xiaomi box 4S
Electron official docs series: Get Started
National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
Electron official docs series: Testing And Debugging
Goto statement to realize shutdown applet
. Net Maui performance improvement
单例的常用创建和使用方式
Go 结构体方法
I - Dollar Dayz
P2393 yyy loves Maths II
Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
P5733 【深基6.例1】自动修正
710. random numbers in the blacklist
This function has none of deterministic, no SQL solution