c++单例类
来源:互联网 发布:一品威客网 知乎 编辑:程序博客网 时间:2024/06/05 10:17
//singleton.h
#pragma once
template <typename T>
class Singleton
{
public:
template<typename... Args>
static T* Instance(Args&&... args)
{
if (m_pInstance == nullptr)
{
m_pInstance = new T(std::forward<Args>(args)...);
}
return m_pInstance;
}
static T* GetInstance()
{
if (m_pInstance == nullptr)
{
throw std::logic_error("Please initialize the instance first");
}
return m_pInstance;
}
static void DestroyInstance()
{
delete m_pInstance;
m_pInstance = nullptr;
}
private:
Singleton(void);
virtual ~Singleton(void);
//将拷贝构造函数和=操作符设置为私有的,禁止外在调用拷贝
Singleton(const Singleton&);
Singleton& operator = (const Singleton&);
private:
static T* m_pInstance;
};
template < class T> T* Singleton<T>::m_pInstance = nullptr;
////使用方式
//class A
//{
//public:
// void test(){};
//};
////使用方式
//Singleton<A>::Instance();
//Singleton<A>::GetInstance()->test();
//Singleton<A>::DestroyInstance();
0 0
- objective-c单例类实现
- objective-c单例类实现
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- des两种加密方式讲解和测试
- IntelliJ IDEA 2016.3.3 配置Tomcat 运行Web项目
- 8VC Venture Cup 2017 - Elimination RoundC. PolandBall and Forest(并查集)
- redis服务启动报未知错误--UnKnown error
- LeetCode--476. Number Complement
- c++单例类
- jdbcType与javaType的对应关系
- opencv学习(二十八)之基本图像阈值操作threshold
- 小程序(之音乐播放器实现思路)
- VS2015 + Vmvare 调试驱动
- (c++)写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。
- 数据分析领域中最为人称道的七种降维方法
- nginx配置长连接---keepalive相关
- 2016迟到的总结