当前位置:网站首页>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;
}边栏推荐
- HDU 3709 Balanced Number
- 倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器
- Deeply analyze the differences between dangbei box B3, Tencent Aurora 5S and Xiaomi box 4S
- RSS rendering of solo blog system failed
- 四类线性相位 FIR滤波器设计 —— MATLAB源码全集
- First knowledge - Software Testing
- Splunk iowait 报警的解决
- tauri vs electron
- 倍福PLC实现绝对值编码器原点断电保持---bias的使用
- How does easygbs solve the abnormal use of intercom function?
猜你喜欢

processing 随机生成线动画

使用SSH密钥对登陆服务器

详细讲解C语言11(C语言系列)

. Net Maui performance improvement

倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用

National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair

第01章_Linux下MySQL的安装与使用

openlayers 绘制动态迁徙线、曲线

记一次phpcms9.6.3漏洞利用getshell到内网域控

Chapter 01_ Installation and use of MySQL under Linux
随机推荐
OPLG: 新一代云原生可观测最佳实践
Electron official docs series: Best Practices
Learning Processing Zoog
倍福PLC基于CX5130实现数据的断电保持
sql 将数据表b字段值赋值到数据表a中某一列
C# const详解:C#常量的定义和使用
第十章 设置结构化日志记录(二)
Explain C language 10 in detail (C language series)
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统
机器学习笔记 - 时间序列的季节性
J - Wooden Sticks poj 1065
倍福TwinCAT3实现CSV、TXT文件读写操作
PostGIS calculation angle
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
find及du -sh显示权限不够的解决方法
.NET MAUI 性能提升
体现技术深度(无法速成)