//new 、delete 操作符的重载 (全局方式)
#include<iostream>
#include<malloc.h>
using namespace std;
class MyClass
{
public:
MyClass(int len,int wid){
length=len;
width=wid;
}
void disp(){
cout<<"面积 :"<<length*width<<endl;
}
void * operator new(size_t size);
void operator delete(void * p);
private:
int length,width;
};
void * MyClass::operator new(size_t size){
cout<<"重载new运算符分配内存。\n";
return malloc(size); //分配size大小内存空间
}
void MyClass::operator delete(void * p){
cout<<"重载delete运算符释放内存。\n";
free(p); //释放p指向的内存空间
}
int main()
{
MyClass *p=new MyClass(10,20);
p->disp();
delete p;
return 0;
}