???????????????????????????
???????????? ???????[ 2013/6/17 10:12:04 ] ????????
???????C++????????????????????????????????????????????????????????
???????????????????????????????????????private????????????????????????????????????á??????????????????private??????????????д?????????????????
??????????????????????????????operator new??operator delete?????private?????????new????????????????????????operator new??delete???????????????operator delete??
#include <iostream>
using namespace std;
//????????????????????
class CHeapOnly
{
public:
CHeapOnly()
{
cout << "Constructor of CHeapOnly!" << endl;
}
void Destroy() const
{
delete this;
}
private:
~CHeapOnly()
{
cout << "Destructor of CHeapOnly!" << endl;
}
};
//????????????????????????????new??????????operator new??л?
class CStackOnly
{
public:
CStackOnly()
{
cout << "Constructor of CStackOnly!" << endl;
}
~CStackOnly()
{
cout << "Destrucotr of CStackOnly!" << endl;
}
private:
void* operator new(size_t size)
{
}
void operator delete(void * ptr)
{
}
};
int main()
{
//use of CHeapOnly
CHeapOnly* pHeap = new CHeapOnly;
pHeap->Destroy();
//error use of CHeapOnly
//CHeapOnly objHeap;
//use of CStackOnly
CStackOnly objStack;
//error use of CStackOnly
//CStackOnly* pStack = new CStackOnly;
return 0;
}
???????????????????????????????????????????????????????private???????????????static???????????????????????????????е??
#include <iostream>
using namespace std;
//????????????????????
class FinalClass
{
public :
static FinalClass* GetInstance()
{
cout<<"Constructor of the class"<<endl;
return new FinalClass;
}
static void DeleteInstance(FinalClass* pInstance)
{
cout<<"Destructor of the class"<<endl;
delete pInstance;
pInstance = 0;
}
private :
FinalClass() {}
~FinalClass() {}
};
int main()
{
//use of CHeapOnly
FinalClass* fc = FinalClass::GetInstance();
FinalClass::DeleteInstance(fc);
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