????????????C++?汾?????
???????????? ???????[ 2015/9/18 13:43:45 ] ??????????????????? .NET
??????????????????ο? C++???????????????????????????μ?????????????л???????????????£??????????????????????????????????С??
????????????????????????????????????????????????????????????????????????鹲?????????????÷?Χ???????????????????????????????????PC???????????
?????????????????????????????????????????????й???????????????C++????????????????Win7 64λ??VS2010??????????
????????ο??????е????????????о?????????????????????CSingleton?????????????
????1. ?????????????????CSingleton g_instance???????????????????????????????????????????????????CSingleton?????????
????2. ?????????????????????????е?????????????static CSingleton1 *m_pInstance;?????????????????????????????????????????????????
????3. ?????????????????????????е??????????static CSingleton1 m_pInstance;
????????????????????????????
????1. ?????????????? ??????????????????????????????????????????????????????
????2. ????????????????????main??????????????????????????????????????????????????????????????? CSingleton1* CSingleton::m_pInstance = NULL??????????????????????????????????????????????????????????д????
????4. ??????????????main???????????CRT???????????У????????????????????????????????????????????????迼?????????????main???????????????????С?
????3. ?????????????????????????????????????????????????????????????????????????????::
?????????????????????????????????????
?????????????????????????????????????????????????г??????????????????????????????????????????????????????????????????
class CSingleton
{
private:
CSingleton() //??????????е?
{
}
CSingleton(const CSingleton &);
CSingleton & operator = (const CSingleton &);
public:
static CSingleton & GetInstance()
{
static CSingleton instance; //??????????
return instance;
}
};
?????????????????????????????з?????????????????????????ο?C++???????????е?????????????????????????????й?????????????????д?????????????????
// singlethon.cpp : ??????????ó????????
#include <iostream>
#include <Windows.h>
using namespace std;
//???????CRT ???й????????
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
//????????????
class Lock
{
private:
CRITICAL_SECTION m_cs; // ????????
Lock(){};
Lock(const Lock&){};
Lock& operator=(const Lock&){};
public:
Lock(CRITICAL_SECTION cs):m_cs(cs)
{
InitializeCriticalSection(&m_cs);
}
void StartLock()
{
EnterCriticalSection(&m_cs);
}
void StopLock()
{
// ???????
//LeaveCriticalSection(&m_cs);
}
~Lock()
{
// ??????? ???????? ò??????
LeaveCriticalSection(&m_cs);
// ??????????????????????????????
DeleteCriticalSection(&m_cs);
}
};
//???????ο???????????????
class Singlethon
{
//??????????Singlethon???????????????????
class CGarbo //??????????????????У????Singlethon?????
{
public:
CGarbo()
{
cout << "constructor CGarbo"<<endl;
}
~CGarbo()
{
if(Singlethon::m_pInstance)
{
cout << "execute CGarbo destructor function"<<endl;
delete Singlethon::m_pInstance;
}
}
};
private:
Singlethon(){cout << "constructor Singlethon "<<endl;}; //?????????? Singlethon single;
Singlethon(const Singlethon&){}; //?????????? Singlethon single2(*(Singlethon::GetInstance()));
// ???? Singlethon single2 = (*(Singlethon::GetInstance()));
Singlethon& operator=(const Singlethon&){}; //?????????? Singlethon single;single = *(Singlethon::GetInstance());
static Singlethon* m_pInstance;
static CGarbo Garbo; //???????????????????????????????????????????
static CRITICAL_SECTION cs;
public:
static Singlethon* GetInstance();
~Singlethon()
{
cout << "execute Singlethon destructor function"<<endl;
//?????????????????????CGarbox??????????????????????????У?????????????????? delete Singlethon::m_pInstance;
//?????Singlethon?????????????????????????ε??? delete Singlethon::m_pInstance??????????????
//???????????????
#if 0
if(Singlethon::m_pInstance)
delete Singlethon::m_pInstance;
Singlethon::m_pInstance = NULL;
#endif
}
};
//???????仰????????
Singlethon::CGarbo Singlethon::Garbo;
Singlethon* Singlethon::m_pInstance = NULL;
Singlethon* Singlethon::GetInstance()
{
if (NULL == m_pInstance)
{
Lock lock(cs);
lock.StartLock();
if (NULL == m_pInstance)
{
m_pInstance = new Singlethon;
}
lock.StopLock();
} //???????????????????????????????lock???????????????lock??????????????
return m_pInstance;
}
int main()
{
//????CRT???й??????
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
Singlethon* p1 = Singlethon::GetInstance();
Singlethon* p2 = Singlethon::GetInstance();
if ( p1 == p2)
{
cout << "????????????!!"<<endl;
}
else
{
cout << "?????????????!!"<<endl;
}
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