其他分享
首页 > 其他分享> > Singleton

Singleton

作者:互联网

原文链接:http://www.cnblogs.com/cxrs/archive/2009/11/17/1604906.html

#include <memory>
#include <iostream>

using namespace std;

template <class T>

class Singleton
{
public:
     static inline T* instance()
    {
        if( 0== _instance.get())
       {
           _instance.reset ( new T);
       }
       return _instance.get();
 };
private:
     Singleton(void){};
     ~Singleton(void){};
     Singleton(const Singleton&){};
     Singleton & operator= (const Singleton &){};
     static auto_ptr<T> _instance;
};

#define DECLARE_SINGLETON_CLASS(type) \
friend class auto_ptr<type>;\
friend class Singleton<type>;

#define IMPLEMENT_SINGLETON_CLASS(type)\
auto_ptr<type> Singleton<type>::_instance;


class TestSingleton
{
public:
     void Run()
     {
      //do some thing
     }
private:
     TestSingleton(void){}
     virtual ~TestSingleton(void){}
     DECLARE_SINGLETON_CLASS(TestSingleton);
};
IMPLEMENT_SINGLETON_CLASS(TestSingleton);

 

 

int _tmain(int argc, _TCHAR* argv[])
{

     Singleton<TestSingleton>::instance()->Run();
     return 0;
}

 

转载于:https://www.cnblogs.com/cxrs/archive/2009/11/17/1604906.html

标签:SINGLETON,void,Singleton,instance,TestSingleton,class
来源: https://blog.csdn.net/weixin_30919919/article/details/97564771