#include <cstdlib>
#include <iostream>
#include<memory>
#include<cstddef>
using namespace std;
class Temp
{
public:
void * operator new(size_t size,int i) //不管是否加上static修饰词,new,delete都将为静态成员函数
{
if(i>10) throw 11;
cout<<"分配内存"<<endl;
int *p=new int[i];
return p;
}
static void operator delete(void *s)
{
cout<<"释放内存"<<endl;
::operator delete (s);
}
};
int main(int argc, char *argv[])
{
int i=11;
int *s;
Temp t;
while(i>10){
try
{
s=new int(i);
i--;
delete(s);
}catch(int)
{
cout<<"run out of memory"<<endl;
}
catch(...)
{
cout<<"捕获任何异常"<<endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
上述程序中为什么我用s=new int(i);没有调用重载运算符啊!而是直接掉用new动态分配内存呢 |