单件模型 Singleton

#include <memory>
using namespace std;

template <class T>
class Singleton

public: 
 static T* instance();
 
private: 
 Singleton(void){} 
 virtual ~Singleton(void){}
 Singleton(const Singleton&){} 
 Singleton & operator= (const Singleton &){} 
 
 static auto_ptr<T> _instance; 
};

template <class T>
auto_ptr<T> Singleton<T>::_instance;

template <class T>
T* Singleton<T>::instance()

 if( 0== _instance.get())  
 { 
  _instance = auto_ptr<T>(new T());
  
 }
 return _instance.get(); 
};

#define DECLARE_SINGLETON(ClassName);/
public: /
 static ClassName* instance()/
{/
 if( 0== _instance.get())/
{/
 _instance = auto_ptr<ClassName>(new ClassName());/
}/
 return _instance.get(); /
}/
private: /
 ClassName(void); /
 ClassName(const ClassName&); /
 ClassName& operator= (const ClassName &); /
static auto_ptr<ClassName> _instance;

#define IMPLEMENT_SINGLETON(ClassName) /
auto_ptr<ClassName> ClassName::_instance(NULL);


class CMyTest
{
DECLARE_SINGLETON(CMyTest)
public:
 void testcout()
 {
  printf("Hello World!/n");
 }
protected:
private:
};

IMPLEMENT_SINGLETON(CMyTest)
CMyTest::CMyTest()
{

}


int main(int argc, char* argv[])
{

 CMyTest::instance();

 CMyTest::instance()->testcout();

 return 0;
}

阅读更多
文章标签: class include null
个人分类: C/C++ WINDOWS 开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭