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> ...
随机推荐
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---观察者模式之WeatherReport[转]
1 2{<HeadFirst设计模式>之观察者模式 } 3{ 主题与观察者 } 4{ 编译工具 :Delphi7.0 ...
- springboot实现定时任务的方式
springboot实现定时任务的方式 a Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程 ...
- pytorch dataloader 取batch_size时候 出现bug
1.RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 342 and 2 ...
- 谷歌浏览器flash被禁用解决方法
谷歌浏览器访问设置:chrome://settings/content/flash 把要启动flash插件的网址添加进去
- vim 简明教程(转自飘过的小牛)
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- LINQ(语言集成查询)
LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作 ...
- Spring这棵大树
目前项目中用的框架大多数都是Spring,一直想找时间对这个框架做一个全面的了解. 今天先以导图的形式画出来Spring都包含的主要模块,即使有些模块目前用不上,但说不定在将来的应用场景时想到Spri ...
- ORACLE时间常用函数(字段取年、月、日、季度)
TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal tw ...
- wpf绑定元素属性
- I Hate It HDU - 1754 (线段树)
注意点:scanf中使用%c时,会读取空格和回车,所以在%c之前要有一个空格 ( 或者直接使用%s也行,%s会忽略空格和回车 ).具体见下面的代码: #include<iostream> ...