C++中delete this
Is it legal (and moral) for a member function to say delete this
?
As long as you’re careful, it’s okay (not evil) for an object to commit suicide (delete
this
).
Here’s how I define “careful”:
- You must be absolutely 100% positively sure that
this
object was allocated vianew
(not bynew[]
, nor by placementnew
, nor a local object on the stack, nor a namespace-scope / global, nor a member of another object; but by plain ordinarynew
). - You must be absolutely 100% positively sure that your member function will be the last member function invoked on
this
object. - You must be absolutely 100% positively sure that the rest of your member function (after the
delete
this
line) doesn’t touch any piece ofthis
object (including calling any other member functions or touching any data members). This includes code that will run in destructors for any objects allocated on the stack that are still alive. - You must be absolutely 100% positively sure that no one even touches the
this
pointer itself after thedelete
this
line. In other words, you must not examine it, compare it with another pointer, compare it withnullptr
, print it, cast it, do anything with it.
Naturally the usual caveats apply in cases where your this
pointer is a pointer to a base class when you don’t have a virtual destructor.
如果在析构函数中调用delete this,则会陷入死循环:
class TDel
{
public:
TDel()
{
x = ;
}
virtual ~TDel()
{
printf("This is Dstr\n");
printf("delete this now\n");
delete this;
}
private:
int x;
}; int main()
{
TDel *td = new TDel;
delete td;
}
结果如下:
This is Dstr
delete this now
This is Dstr
delete this now
This is Dstr
delete this now
This is Dstr
delete this now
This is Dstr
delete this now
...
https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
C++中delete this的更多相关文章
- MYSQL中delete删除多表数据
MYSQL中delete删除多表数据 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 ...
- C++ 中 delete 和 delete[] 的区别
一直对 C++ 中 delete 和 delete[] 的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++ 告诉我们在回收用 new 分配的单个对象的内存空间时用 de ...
- vim中delete(backspace)键不能向左删除
MacOS修改为英文语言之后,忽然出现如题问题:delete键不能向左删除,只可以删除本次插入模式下插入的文本.原因: 转载:https://www.smslit.top/2016/11/27/vim ...
- oracle中delete、truncate、drop的区别
oracle中delete.truncate.drop的区别 标签: deleteoracletable存储 2012-05-23 15:12 7674人阅读 评论(0) 收藏 举报 分类: ora ...
- 请说明SQLServer中delete from tablea & truncate table tablea的区别
请说明SQLServer中delete from tablea & truncate table tablea的区别 解答:两者都可以用来删除表中所有的记录.区别在于:truncate是DDL ...
- 【转】 C++中delete和delete[]的区别
一直对C++中的delete和delete[]的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete ...
- C++中delete[]是如何知道数组大小的
先看一段代码: int main(void) { int *pI = new int; int *pArray = new int[10]; int size = *(pArray-1); delet ...
- C++中delete和delete[]的区别
C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]. 关于 new[] 和 delete[],其中又 ...
- C++中delete和 delete[]的区别
总的原则是,如果是用new[]创建的,则用delete[]删除,如果是用new创建的,则用delete删除. 对于基本类型,比如char *p=new char[20];如果删除时,delete p和 ...
- C++中delete 和delete[]的区别
c++告诉我们在回收new分配的单个对象的内存空间的时候用delete, 回收new[ ]分配的一组对象的内存空间的时候用 delete[ ]; #include <iostream> ...
随机推荐
- docker 整理
管理 docker批量删除容器.镜像 1.删除所有容器 docker rm `docker ps -a -q` 1.1 按条件删除容器 删除包含某个字段 ,镜像名或容器名均可, 例如删除 zhy* ...
- vue项目 环境搭建
1.前端安装 安装项目:vue init webpack docvote 进入docvote里:cd docvote 安装脚手架:cnpm i 运行:npm run dev 2.异步加载 const ...
- js中在window.onload中写function函数产生的问题
转载自:https://blog.csdn.net/c_p_h/article/details/63684510 今天在jsp中的form表单利用οnclick=”xxx”来监听js中一个functi ...
- ajax返回后台编译时都对,返回error
首先看看你的dataType:‘json’ 类型是否与后台获取的类型一直.特别是json的格式对不对. 第二: 红括号里的有没有加
- CentOS 6.8 Linux系统U盘制作启动项
1.下载CentOS 6.8镜像文件: 2.下载地址:http://man.linuxde.net/download/CentOS_6_8 3.准备一个U盘,最好8G的: 4.下载UltraISO盘制 ...
- 一、WebService基础概念
一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intrane ...
- Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...
- java窗体swing使用jlabel显示图片
Icon icon = new ImageIcon("src\\resource\\" + jTFimgName.getText()); jLabColor.setIcon(ico ...
- nginx链接末尾自动补全斜杠
放在locaation里边就行 if (-d $request_filename){ rewrite ^(.*[^/])$ $/ permanent;#加斜杠 } 这样,nginx就会进行判断了,如果 ...
- day18 13.乐观锁介绍
乐观锁是使用版本字段,悲观锁是使用数据库底层的锁机制.mysql的类型timestamp(时间戳)有一个特点:插入数据不用管我,我取系统当前默认值.timestamp插入时间会记录,修改时间也会记录. ...