????????C++???
???????????? ???????[ 2014/3/6 9:41:13 ] ??????????????? C++ ?????? Public
????????????????????????????ó??????????????????????????????????????ó???????????????????????????????????????????????????????????Щ????£?????????????????????????
????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;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11