/** @file Singleton.h * @note * @brief 宏定义一个类实现单例需要做的一些工作。 * @author Jlzlight * @date 2011-09-27 * @note 每个单例类最好将其构造函数和虚析构函数设置为private */ #include <new> #define SINGLETON_DECLARE(theclass) \ public: \ static theclass * GetSingletonPtr();\ static void FreeSingleton(); \ private: \ static theclass * m_s##theclass #define SINGLETON_IMPLEMENT(theclass) \ theclass * theclass::m_s##theclass = NULL; \ theclass * theclass::GetSingletonPtr() \ { \ 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; \ } \ } \ //
class ClassA { private: ClassA(void); ~ClassA(void); SINGLETON_DECLARE(ClassA); }; SINGLETON_IMPLEMENT(CAppSession); CAppSession::CAppSession(void); ClassA::ClassA() {} ClassA::~ClassA() {}
单例模式
最新推荐文章于 2019-10-29 10:58:59 发布