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
thisobject 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
thisobject. - You must be absolutely 100% positively sure that the rest of your member function (after the
deletethisline) doesn’t touch any piece ofthisobject (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
thispointer itself after thedeletethisline. 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> ...
随机推荐
- 在scrapy中利用Selector来提取数据
1.创建对象 Selector类的实现位于scrapy.selector模块,创建Selector对象的时候,可以将页面的Html文档字符串传递给Selector构造器方法 2.选中数据 调用Sele ...
- 免费提取百度文库 doc 文件
首先说明,今天要推荐的这款软件,不能不能不能免费提取百度文库里 PDF 格式的文件. 对于其他的格式,无论收费与否都能免费提取. 只是口头说说免不了耍流氓的嫌疑,举栗如下: 百度文库里<喜迎党的 ...
- php匿名函数与闭包函数
匿名函数:没有名字的函数:并没有牵扯到应用其他函数的变量问题.仅仅是没有名字 $f=function($param){} 闭包:A函数中嵌套着B函数,B程序中有用到A的变量,当外部函数C调用函数A时, ...
- HDFS数据读写过程
- 【Java爬虫】爬取南通大学教务处成绩
没使用自动登录,所以获取是比较麻烦.. 1.http://jwgl.ntu.edu.cn/cjcx 进入官网,进行账号密码登录 2.点击全部成绩查询(也一定要点进去,不然cookie不会返回值) ...
- 通过游戏学python 3.6 第一季 第四章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释 可复制直接使用 娱乐 可封装 函数
#猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释 #!usr/bin/env python #-*-coding:utf-8-*- #QQ124111294 import ...
- WPF 实现简单的跑马灯
本文用WPF的动画实现一个简单的跑马灯 xmal: <Window x:Class="wpfstatusBar.MainWindow" xmlns="http:// ...
- java-Map-system
一 概述 0--星期日1--星期一... 有对应关系,对应关系的一方是有序的数字,可以将数字作为角标. public String getWeek(int num){ if(num<0 || n ...
- dom元素分屏加载
载入一个内容较多的页面的时候,如果不做任何处理,则会在一开始就把全部内容加载出来,影响了性能和体验,所以现在常用分屏加载的功能,就是dom内容出现在屏幕可视范围内的时候,再加载dom节点,起到优化的作 ...
- 洛谷P1072 [NOIP2009] Hankson 的趣味题
P1072 Hankson 的趣味题 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一 ...