C++:单例模式:修订间差异

来自WHY42
imported>Soleverlee
(以“这是CEGUI源码中的单例模式实现模版: <source lang="cpp> #include <cassert> template <typename T> class CEGUIEXPORT Singleton { protected: static #i...”为内容创建页面)
 
imported>Soleverlee
无编辑摘要
第1行: 第1行:
这是CEGUI源码中的单例模式实现模版:
这是CEGUI源码中的单例模式实现模版:
<source lang="cpp>
<source lang="cpp">
#include <cassert>
#include <cassert>
template <typename T> class CEGUIEXPORT Singleton
template <typename T> class CEGUIEXPORT Singleton

2015年11月7日 (六) 06:37的版本

这是CEGUI源码中的单例模式实现模版:

#include <cassert>
template <typename T> class CEGUIEXPORT Singleton
{
protected:
    static
#ifdef __MINGW32__
    CEGUIEXPORT
#endif
    T* ms_Singleton;

public:
    Singleton( void )
    {
        assert( !ms_Singleton );
        ms_Singleton = static_cast<T*>(this);
    }
   ~Singleton( void )
        {  assert( ms_Singleton );  ms_Singleton = 0;  }
    static T& getSingleton( void )
        {  assert( ms_Singleton );  return ( *ms_Singleton );  }
    static T* getSingletonPtr( void )
        {  return ( ms_Singleton );  }

private:
    Singleton& operator=(const Singleton&) { return this; }
    Singleton(const Singleton&) {}
};