????(2) delete opeartor
????[::] delete cast-expression
????[::] delete [ ] cast-expression
??????????delete operator??????????????????????????(???????????????????棬???????????????????????????檔??C++?У???new operator?????????棬???????delete operator???????????delete operator??????????????????????????
????a. ?????????????????????????
????b. ????operator delete function????????(deallocate the memory)
????3. ????new/delete??ù??????Щ????????
????(1)???????operator new/delete function ?? new/delete operator ?
?????????????????????????????????/???????棬???????new/delete operator?? ???????new/delete??????У????????????????operator new/delete function???????????????/???????
????(2) ??delete operator??????鯔????new operator??????棬??????????????????м??operator new??operator delete???????????????д?ó???????
????(3) new operator???????????std::bad_alloc?????????????????????????operator new function??delete operator?????????????????delete???????
????(4) ????????汻delete?????????????(Dereference)???????????????????????3??????
????(5) delete?????(NULL)??????????????κκ?????
????(6) ?????????operator new/delete????????????(static)??????????????????麯??(virtual function)???????public?? protected?? private???????????
????4. ????????????????????Щ?????
????????
#include <stdio .h>
#include <stdlib .h>
void * operator new(size_t unSize)
{
printf("operator new called ");
return malloc(unSize);
}
void * operator new(size_t unSize?? int nLine?? const char * pFunc)
{
printf("operator new called?? line: %d?? func: %s "??
nLine?? pFunc);
return malloc(unSize);
}
void operator delete(void * pMem)
{
printf("delete1 ");
free(pMem);
}
class A
{
public:
A(int a = 0) :
_a(a)
{
printf("constructor called ");
}
{
printf("~A() ");
}
static void operator delete(void * pMem?? size_t unSize)
{
printf("delete2: %u "?? unSize);
free(pMem);
}
private:
int _a;
};
class B: public A
{
public:
~B()
{
printf("~B() ");
}
int _b;
int _bb;
};
int main()
{
A * pA = new A(10);
printf("####### ");
A * pB = new (__LINE__?? __func__) B();
printf("####### ");
A * szA = new A[10];
printf("####### ");
delete pA;
printf("####### ");
delete pB;
printf("####### ");
delete [] szA;
printf("####### ");
char * pC = NULL;
delete pC;
}