定义类的全局唯一示例对象,带有作用域的全局类对象,采用static实现。
代码示例:
class Singleton
{
public:
static Singleton *getInstance()
{
if(_singleton == nullptr)
{
//加锁
if(_singleton == nullptr)
_singleton = new Singleton;
}
}
static void disInstance()
{
//加锁
if(_singleton != nullptr)
{
delete _singleton;
_singleton = nullptr;
}
}
private:
Singleton();
static Singleton *_singleton = nullptr;
};