#include <iostream>
using namespace std;
template<class T>
class Singleton
{
public:
static T* CreateInstance();
static void Release();
protected:
Singleton(){}
virtual ~Singleton(){}
private:
Singleton(const Singleton&){}
Singleton& operator=(const Singleton&){}
private:
static T* m_pT;
};
template<class T>
T* Singleton<T>::m_pT = NULL;
template<class T>
T* Singleton<T>::CreateInstance()
{
if (m_pT == NULL)
{
m_pT = new T;
}
return m_pT;
}
template<class T>
void Singleton<T>::Release()
{
if (m_pT != NULL)
{
delete m_pT;
m_pT = NULL;
}
}
class PA : public Singleton<PA>
{
friend class Singleton<PA>;
private:
PA(){}
virtual ~PA(){}
};
int main()
{
PA* q = PA::CreateInstance();
PA* p = Singleton<PA>::CreateInstance();
PA::Release();
return 0;
}