????????????????????????????ó??????????????????????????????????????ó???????????????????????????????????????????????????????????Щ????£?????????????????????????
????Singleton?????????????????
????1??????????????????????????????Public?????????????????????????????????????Instance??Public??????????????????????????????????????????????
????2???????????????Private????????????"????"???????κ??????ù?????????????????????????????????????????????????Instance??????????????????
#include <iostream>
#include <cassert>
using namespace std;
template<typename T>
class Singleton
{
public:
static T *getInstancePtr()
{
if (m_Instance == NULL) {
m_Instance = new T;
}
return m_Instance;
}
static T& getInstance()
{
assert(m_Instance);
return (*m_Instance);
}
private:
Singleton(){};
static T *m_Instance;
};
template<typename T> T * Singleton<T>::m_Instance = NULL;
class A
{
public:
void display(){cout<<"A is display"<<endl;}
};
int main()
{
/*Singleton<int>::getInstancePtr();
cout<<Singleton<int>::getInstance()<<endl;
Singleton<int>::getInstance()=5;
cout<<Singleton<int>::getInstance()<<endl;*/
A* p=Singleton<A>::getInstancePtr();
p->display();
return 0;
}