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> ...
随机推荐
- CentOS 6.5 Apache+SVN配置
yum -y install subversion #安装SVN svnserve -- version #查看svn版本信息确定是否安装 yum -y install httpd #安装Apache ...
- Action详解
简介 Action 是用于处理请求操作的,它是由 StrutsPrepareAndExecuteFilter 分发过来的. 在 Struts2 框架中,Action 是框架的核心类,被称为业务逻辑控制 ...
- docker和宿主机之间复制文件
从主机复制到容器sudo docker cp host_path containerID:container_path 从容器复制到主机sudo docker cp containerID:conta ...
- jmeter-监听器介绍与使用
12.jmeter-监听器介绍与使用 jmeter-监听器介绍与使用 察看结果树 Summary Report 聚合报告 Backend Listener Aggregate Graph 断言结果 C ...
- python中常见的错误
python中常见的错误 1.IndentationError: unindent does not match any outer indentation leve 众所周知,Python语法要 ...
- maximum clique 1
maximum clique 1 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288KSpecial Judge, 64bit IO Format: % ...
- PHP获取搜索引擎关键词
有时候我们需要知道用户通过哪个搜索引擎,通过拿个关键词访问我们页面,当然js也可以实现,这里介绍下php的实现代码,包含(百度.谷歌.雅虎.搜狗.搜搜.必应.有道)几大搜索引擎的获取方法. //获取来 ...
- mybatis深入理解(七)-----MyBatis缓存机制的设计与实现
缓存设计 MyBatis将数据缓存设计成两级结构,分为一级缓存.二级缓存: 一级缓存是Session会话级别的缓存,位于表示一次数据库会话的SqlSession对象之中,又被称之为本地缓存.一级缓存是 ...
- cvc-elt.1: 找不到元素 'beans' 的声明。springmvc netbeans maven
搭建最基本的框架,出现问题,提示cvc-elt.1: 找不到元素 'beans' 的声明. HTTP Status 500 - Servlet.init() for servlet spring th ...
- html转换成pdf
指定html转换成pdf 安装插件: npm install --save html2canvas npm install jspdf --save 引入 plugins/ htmlToPdf.js ...