Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered.

  (1)delete operator works only for objects allocated using operator new (See http://geeksforgeeks.org/?p=8539).

  If the object is created using new, then we can do delete this, otherwise behavior is undefined.

 1 class A
2 {
3 public:
4 void fun()
5 {
6 delete this;
7 }
8 };
9
10 int main()
11 {
12 /* Following is Valid */
13 A *ptr = new A;
14 ptr->fun();
15 ptr = NULL // make ptr NULL to make sure that things are not accessed using ptr.
16
17
18 /* And following is Invalid: Undefined Behavior */
19 A a;
20 a.fun();
21
22 getchar();
23 return 0;
24 }

  (2)Once delete this is done, any member of the deleted object should not be accessed after deletion.

 1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 int x;
7 public:
8 A()
9 {
10 x = 0;
11 }
12 void fun()
13 {
14 delete this;
15
16 /* Invalid: Undefined Behavior */
17 cout << x;
18 }
19 };
20 int main()
21 {
22 A *a = new A;
23 a->fun();
24
25 return 0;
26 }

  Output: 一个随机数

  The best thing is to not do delete this at all.

  Thanks to Shekhu for providing above details.

  References:
      https://www.securecoding.cert.org/confluence/display/cplusplus/OOP05-CPP.+Avoid+deleting+this
      http://en.wikipedia.org/wiki/This_%28computer_science%29

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  09:42:47

"delete this" in C++的更多相关文章

  1. 如何区别数据库删除语句drop与delete与truncate?

    1.delete:删除数据表中的行(可以删除某一行,也可以在不删除数据表的情况下删除所有行) 删除某一行:delete from 数据表名称 where 列名称=值: 删除所有行:delete*fro ...

  2. 数据库设计中的Soft Delete模式

    最近几天有点忙,所以我们今天来一篇短的,简单地介绍一下数据库设计中的一种模式——Soft Delete. 可以说,该模式毁誉参半,甚至有非常多的人认为该模式是一个Anti-Pattern.因此在本篇文 ...

  3. 关于JavaScript中的delete操作

    关于JavaScript中的delete操作 看到一道题,是这样的: (function(x){ delete x; return x; })(1); 1 null undefined Error 我 ...

  4. Git异常:Cannot delete the branch 'test1' which you are currently on

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  5. HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)

    前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...

  6. IIS7.5上的REST服务的Put,Delete操作发生HTTP Error 405.0 - Method Not Allowed 解决方法

    WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...

  7. ASP.NET Core 中文文档 第二章 指南(4.10)检查自动生成的Detail方法和Delete方法

    原文 Examining the Details and Delete methods 作者 Rick Anderson 翻译 谢炀(Kiler) 校对 许登洋(Seay).姚阿勇(Mr.Yao) 打 ...

  8. new/delete重载

    在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...

  9. EC笔记:第三部分:16成对使用new和delete

    我们都知道,申请的资源,使用完毕后要释放.但是这个释放动作,一定要注意. 举个例子,很多人动态分配的资源,在使用之后,往往直接调用了delete,而不管申请资源的时候用的是new还是new[]. 如下 ...

  10. Spring boot: Request method 'DELETE' not supported, Request method 'PUT' not supported, Request method 'POST' not supported

    GET,POST,PUT,DELETE, Spring都支持,不要怀疑Spring, 一定是前端发送的rest 请求和后端的响应不匹配, 查找原因以及解决办法, 很简单 用chrome打开F12控制台 ...

随机推荐

  1. Java测试开发--MySql之C3P0连接池(八)

    连接池C3P0! 连接池技术的目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能 ! 下面以案例演示下C3P0的操作流程. 1.测试准备: ①MySql数据库一枚②database名为myte ...

  2. NOIP模拟92&93(多校26&27)

    前言 由于太菜了,多校26 只改出来了 T1 ,于是直接并在一起写啦~~~. T0 NOIP 2018 解题思路 第一次考场上面写三分,然而我并不知道三分无法处理不是严格单峰的情况,但凡有一个平台都不 ...

  3. sql常见题目

    1 --student学生表(sno,sname,sex,birthday,tel) 2 --Course课程表(cno,cname) 3 --Sc 学生成绩表(sno,cno,score) 4 1. ...

  4. Salesforce Consumer Goods Cloud 浅谈篇三之 行动计划(Action Plan)相关配置

    本篇参考: https://v.qq.com/x/page/f0772toebhd.html https://v.qq.com/x/page/e0772tsmtek.html https://v.qq ...

  5. Javascript复制内容到剪贴板,解决navigator.clipboard Cannot read property 'writeText' of undefined

    起因 最近帮同事实现了一个小功能--复制文本到剪贴板,主要参考了前端大神阮一峰的博客,根据 navigator.clipboard 返回的 Clipboard 对象的方法 writeText() 写文 ...

  6. [luogu5162]WD与积木

    设$g_{n}$表示$n$个积木放的方案数,枚举最后一层所放的积木,则有$g_{n}=\sum_{i=1}^{n}c(n,i)g_{n-i}$(因为积木有编号的所以要选出$i$个) 将组合数展开并化简 ...

  7. IDEA修改XML注释风格

    作为一个强迫症患者,每次想在xml文件用快捷键注释的时候,它自动生成的注释一直都是这样的:这令我非常难受,于是每次我都要把光标移到前面,然后再Tab以下,让它变成这样可是每次都这样,好麻烦啊,如果自己 ...

  8. Codeforces 429E - Points and Segments(欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 果然我不具备融会贯通的能力/ll 看到这样的设问我们可以很自然地联想到这道题,具体来说我们可以通过某种方式建出一张图,然后根据" ...

  9. 感谢 git

    今天对程序大修了一下,顺便把所有算例测试了一遍,突然发现二维浅水方程有些算例出现了明显的错误. 这次突然出现的错误让我有点措手不及,因为一直没有修改过浅水方程求解器,所以这些算例很久没有测试过了.硬着 ...

  10. Perl 语言入门1-5

    第一章 简介 perl -v 文字处理,编写小型CGI脚本(Web服务器调用程序)的最佳语言 CPAN: Perl综合典藏网 shebang: #! /usr/bin/perl 或#! /usr/lo ...