风泥

导航

c++使用预定义宏来创建单例实例

#define INSTANCE(type) \

private: \

    static type *m_pInstance; \

public: \

    static type * instance(); \

    static void destroySelf(); \

private:


#define INSTANCE_CPP(type) \

type *type::m_pInstance = NULL ; \

 \

type *type::instance() \

{ \

if(!m_pInstance) \

        m_pInstance = new type(); \

    return m_pInstance; \

} \

 \

void type::destroySelf() \

{ \

    if(m_pInstance) \

        delete m_pInstance; \

    m_pInstance = NULL; \

}


posted on 2015-05-18 19:50  风泥  阅读(121)  评论(0编辑  收藏  举报