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”:

  1. You must be absolutely 100% positively sure that this object was allocated via new (not by new[], nor by placement new, nor a local object on the stack, nor a namespace-scope / global, nor a member of another object; but by plain ordinary new).
  2. You must be absolutely 100% positively sure that your member function will be the last member function invoked on this object.
  3. You must be absolutely 100% positively sure that the rest of your member function (after the delete this line) doesn’t touch any piece of this 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.
  4. You must be absolutely 100% positively sure that no one even touches the this pointer itself after the delete this line. In other words, you must not examine it, compare it with another pointer, compare it with nullptr, 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的更多相关文章

  1. MYSQL中delete删除多表数据

    MYSQL中delete删除多表数据 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 ...

  2. C++ 中 delete 和 delete[] 的区别

    一直对 C++ 中 delete 和 delete[] 的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++ 告诉我们在回收用 new 分配的单个对象的内存空间时用 de ...

  3. vim中delete(backspace)键不能向左删除

    MacOS修改为英文语言之后,忽然出现如题问题:delete键不能向左删除,只可以删除本次插入模式下插入的文本.原因: 转载:https://www.smslit.top/2016/11/27/vim ...

  4. oracle中delete、truncate、drop的区别

    oracle中delete.truncate.drop的区别 标签: deleteoracletable存储 2012-05-23 15:12 7674人阅读 评论(0) 收藏 举报  分类: ora ...

  5. 请说明SQLServer中delete from tablea & truncate table tablea的区别

    请说明SQLServer中delete from tablea & truncate table tablea的区别 解答:两者都可以用来删除表中所有的记录.区别在于:truncate是DDL ...

  6. 【转】 C++中delete和delete[]的区别

    一直对C++中的delete和delete[]的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete ...

  7. C++中delete[]是如何知道数组大小的

    先看一段代码: int main(void) { int *pI = new int; int *pArray = new int[10]; int size = *(pArray-1); delet ...

  8. C++中delete和delete[]的区别

    C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]. 关于 new[] 和 delete[],其中又 ...

  9. C++中delete和 delete[]的区别

    总的原则是,如果是用new[]创建的,则用delete[]删除,如果是用new创建的,则用delete删除. 对于基本类型,比如char *p=new char[20];如果删除时,delete p和 ...

  10. C++中delete 和delete[]的区别

    c++告诉我们在回收new分配的单个对象的内存空间的时候用delete,   回收new[ ]分配的一组对象的内存空间的时候用 delete[ ]; #include <iostream> ...

随机推荐

  1. switch的练习

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. hibernate 多表关联外键问题无法截断表的解决办法

    目前只有一个办法 就是手动清除其他表的外键关联,然后在做一个小swing单独去操作这个表,而不运行主控方相关的代码 当web运行后,外键会再次设置好

  3. 轻松搞定 JS 的this、call和apply

    年前最后一篇文章,提前祝大家春节快乐.对了,还有369就要2018年了,提前祝大家2018年春节快乐. .. .vr qBMBBBMBMY 8BBBBBOBMBMv iMBMM5vOY:BMBBv . ...

  4. 使用Python Requests上传表单数据和文件

    在Python环境下写一个HTTP客户端,发送POST请求,同时上传表单数据和文件,我们可以使用Requests模块来实现.代码如下: data = { 'name': 'nginx' } files ...

  5. python \r \t \n 各种转义字符

    转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...

  6. 20190813「Night」-Blind

    夜场. 先说说题面,周,任,飞? 好像是个巨巨巨巨巨巨佬. 郭神?同上. 好像题解包里都有. %%%出题人liu_runda. liu_runda是谁? 凭实力在NOI2017退役的辣鸡蒟蒻. 郭神是 ...

  7. Laravel-admin之Driver [] is not supported

    使用Laravel-admin做项目,原本好好的项目,今天一运行则报错:Driver [] is not supported,截图如下: 翻看百度翻译之后,才知道是不支持驱动器[],但是知道意思还是不 ...

  8. Spring 集成 Redis

    pom.xml <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  9. Oracle中查看所有的表,用户表,列名,主键,外键

    在Oracle中查看所有的表: select * from tab/dba_tables/dba_objects/cat; 看用户建立的表 : select table_name from user_ ...

  10. JS Ajax跨域访问

    js ajax跨域访问报"No 'Access-Control-Allow-Origin' header is present on the requested resource 如果请求的 ...