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. js事件常用操作、事件流

    注册事件 给元素添加事件,称为注册事件或者绑定事件. 注册事件有两种方式:传统方式和方法监听注册方式 传统方式 on开头的事件,例如onclick <button onclick="a ...

  2. electron获取不到remote

    electron获取不到remote 问题 // 渲染进程 let remote = require('electron').remote console.log(remote) // undefin ...

  3. 一文带你理解TDengine中的缓存技术

    作者 | 王明明,涛思数据软件工程师 小 T 导读:在计算机系统中,缓存是一种常用的技术,既有硬件缓存,比如我们经常听到的 CPU L2 高速缓存,也有软件缓存,比如很多系统里把 Redis 当做数据 ...

  4. 第三天 while循环 及其用法

    (1)语法格式:while  条件: ..... 语法二:while  条件: break  # 跳出当前循环 语法三:while 条件: else  # 当while循环正常结束时执行该语句:只有程 ...

  5. python中jsonpath模块,解析多层嵌套的json数据

    1. jsonpath介绍用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, ...

  6. 菜鸡的Java笔记 - java 线程常用操作方法

    线程常用操作方法        线程的命名操作,线程的休眠,线程的优先级            线程的所有操作方法几乎都在 Thread 类中定义好了            线程的命名和取得      ...

  7. 使用nacos作为配置中心统一管理配置

    基础环境 引入所需依赖包 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...

  8. [luogu7092]计数题

    由于$\mu(i)$,因此每一个素数最多存在1次,当$k=0$答案必然为0 根据莫比乌斯和欧拉函数的积性,答案与对素数的划分无关,仅与每一个素数是否出现有关,换言之枚举素数出现的集合$P'$,答案即为 ...

  9. Cortex-A系列中断

    1. 回顾STM32系统 1.1 中断向量表 ARM芯片冲0x00000000,在程序开始的地方存放中断向量表,按下中断时,就相当于告诉CPU进入的函数.描述很多个中断服务函数的表. 对于STM32来 ...

  10. 网络管理之命令行工具nmcli

    参考Ubuntu官方文档和Red Hat,本文采用Google翻译. NETWORKMANAGER 简介 介绍 NetworkManager 提供的默认联网服务是一个动态网络控制和配置守护进程,它尝试 ...