//通用的头文件
// 宏定义一个类实现单例需要做的一些工作。
// 每个单例类最好将其构造函数和虚析构函数设置为private
#pragma once
#define SINGLETON_DECLARE(theclass) \//头文件类中调用
public: \
static theclass * SnglPtr(); \
static void FreeSingleton(); \
private: \
static theclass * m_s##theclass; \
#define SINGLETON_IMPLEMENT(theclass) \//源文件中的预处理下面调用
theclass * theclass::m_s##theclass = NULL; \
theclass * theclass::SnglPtr() \
{ \
if (NULL == m_s##theclass) \
{ \
m_s##theclass = new(std::nothrow) theclass; \
} \
return m_s##theclass; \
} \
void theclass::FreeSingleton() \
{ \
if (NULL != m_s##theclass) \
{ \
delete m_s##theclass; \
m_s##theclass = NULL; \
} \
} \
//
宏定义C++单例模式
最新推荐文章于 2022-09-17 21:28:51 发布