new delete操作符重载
#include<iostream>#include<cstdlib>usingnamespacestd;classA{public:A(void){cout<<"A::A()"<<endl;}~A(void){cout<<"A::~A()"<<endl;}staticvoid*operatornew(size_tsize){cout<<"A::new"<<endl;void*pv=malloc(size);returnpv;}staticvoidoperatordelete(void*pv){cout<<"A::delete"<<endl;free(pv);}};intmain(void){A*pa=newA;deletepa;return0;}
结果为:
A::new
A::A()
A::~A()
A::delete
由此我们可以知道,new 对象时,先new在调用其构造,
delete对象的时,先调用其析构,再delete
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。