xuejianhui

导航

宏定义一个类为单例模式

// 宏定义一个类实现单例需要做的一些工作。
// 每个单例类最好将其构造函数和虚析构函数设置为private
#pragma once
#include <new>

#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;               \
    }                                       \
}                                           \

//////////////////////////////////////////////////////////////////////////

 

posted on 2014-04-04 16:45  xuejianhui  阅读(593)  评论(0编辑  收藏  举报