30 lines
552 B
C++
30 lines
552 B
C++
#pragma once
|
|
#include "common.hpp"
|
|
|
|
class SingletonA // 懒汉式,线程不安全,空间换时间
|
|
{
|
|
private:
|
|
SingletonA() {} // 构造函数私有化保证对象不会在类外被创建
|
|
|
|
SingletonA(SingletonA&) = delete;
|
|
SingletonA& operator=(const SingletonA&) = delete;
|
|
|
|
static SingletonA* m_pInstance;
|
|
public:
|
|
static SingletonA* getInstance();
|
|
|
|
};
|
|
|
|
class SingletonB // 饿汉式,线程安全,时间换空间
|
|
{
|
|
private:
|
|
SingletonB() {}
|
|
SingletonB(SingletonB&) = delete;
|
|
SingletonB& operator=(const SingletonB&) = delete;
|
|
public:
|
|
static SingletonB* getInstance();
|
|
|
|
};
|
|
|
|
|