重载operator new 和delete

lingg2002 lingg2002 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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()
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
lingg2002
VC/MFC 16,550 成员 421,606 内容