重载operator new 和delete
#include<cstddef>
#include<iostream>
using namespace std;
class S{
public:
S(int=2):a(2){cout<<"S(int=2)\n";}
void *operator new(size_t);
void operator delete (void *);
int a;
~S(){
cout<<"~S()\n";
}
};
void * S::operator new(size_t){
cout<<"void *S:new(size_t)"<<endl;
return ::new S;
}
void S::operator delete(void *a){
cout<<"void *S::delte"<<endl;
}
void main(){
S *aa=new S;
cout<<aa->a<<'\n';
delete aa;
}
构造函数为什么执行两次
为何会在delete aa;中会调用~S(),我重载了delete但是没有调用~S()