#ifndef _SINGLETON_329FC8D0_C954_4cd3_ADE8_B0CB704A35F3_h_
#define _SINGLETON_329FC8D0_C954_4cd3_ADE8_B0CB704A35F3_h_
template<class T>
class Singleton
{
//typedef T ObjectType;
public:
static T & Instance()
{
if(NULL == m_instance)
m_instance = new T;
return *m_instance;
}
protected:
Singleton(){};
private:
static T* m_instance;
};
template<class T>
T* Singleton<T>::m_instance =0;
#define _SINGLETON_329FC8D0_C954_4cd3_ADE8_B0CB704A35F3_h_
template<class T>
class Singleton
{
//typedef T ObjectType;
public:
static T & Instance()
{
if(NULL == m_instance)
m_instance = new T;
return *m_instance;
}
protected:
Singleton(){};
private:
static T* m_instance;
};
template<class T>
T* Singleton<T>::m_instance =0;
#endif
//test
class CPeople
{
public:
int GetAge(){return m_iAge;}
void SetAge(int iAge){m_iAge = iAge;}
protected:
private:
int m_iAge;
};
typedef Singleton<CPeople> singleMgr;
int main(int argc, char* argv[])
{
CPeople * p = &singleMgr::Instance();
if (NULL == p)
return 0;
p->SetAge(11);
printf("age: %d !\n",p->GetAge());
return 0;
}
没有考虑多线程安全情况,请各位大侠指教!@_~