零碎知识

本人实力不济,如有错误或建议及补充,请指出(评论或私信都行)

rt,本文就是一些懒得整理的(或少到整理不起来的)零碎知识(内含部分发癫内容)

析构函数

为啥学呢,

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

在此提供一个错误:

// 析构函数乱搞有时候真的很容易卡很久
#include<bits/stdc++.h>
using namespace std;
struct tmp{
int x;
tmp(){}
~tmp(){while(x++);}
};
template<class T=int>
struct Name{
void *p;
Name(){p=new T;}
~Name(){
((T*)p)->x=1;
printf("%d\n",6);
delete (T*)p;
}
};
int main()
{
//...
Name<tmp> ko[1000];
return 0;
}

附:卡爆的构造函数

#include<bits/stdc++.h>
using namespace std;
struct tmp{
int x;
tmp *p;
tmp(){p=new tmp;x=rand();*(int*)p=rand()*rand();}
~tmp(){
;
}
};
int main()
{
srand(time(0));
tmp ko;
return 0;
}
posted @   AC-13-13  阅读(6)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起