单例 Singleton 是设计模式的一种,其特点是只提供唯一一个类的实例,具有全局变量的特点,在任何位置都可以通过接口获取到那个唯一实例。
#ifndef _INSTANCE_H_
#define _INSTANCE_H_
class Instance
{
private:
Instance(){}
~Instance(){}
Instance(const Instance& copy){}
static Instance* m_pInstance;
public:
static Instance* getInstance();
};
#endif
。
.cpp文件
#include"Instance.h"
Instance* Instance::m_pInstance=nullptr;
Instance* Instance::getInstance()
{
if (m_pInstance==nullptr)
{
m_pInstance = new Instance;
}
return m_pInstance;
}



