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 网络编程 - 总结概述

    IP地址 IP地址IntAddress 唯一定位一台网络上的计算机 127.0.0.1:本地localhost IP地址的分类 ipV4/ipV6 ipV4:127.0.0.1,4个字节组成:0~25 ...

  2. Go语言核心36讲(Go语言实战与应用二)--学习笔记

    24 | 测试的基本规则和流程(下) Go 语言是一门很重视程序测试的编程语言,所以在上一篇中,我与你再三强调了程序测试的重要性,同时,也介绍了关于go test命令的基本规则和主要流程的内容.今天我 ...

  3. SpringCloud config native 配置

    1.概述 最近项目使用springCloud 框架,使用config搭建git作为配置中心. 在私有化部署中,出现很多比较麻烦的和鸡肋的设计. 每次部署都需要安装gitlab 有些环境安装完gitla ...

  4. python一对一教程:Computational Problems for Physics chapter 1 Code Listings

    作者自我介绍:大爽歌, b站小UP主 ,直播编程+红警三 ,python1对1辅导老师 . 本博客为一对一辅导学生python代码的教案, 获得学生允许公开. 具体辅导内容为<Computati ...

  5. cesium开发(1)搭建 vue + cesium开发环境

    进入新公司一段时间了,新公司业务主要从事卫星方面等webgl的开发,主要使用了leafletjs和cesium,其中cesium难度较大,需求较多,再进行了一段时间的使用开发后依旧感到有些力不从心, ...

  6. [loj6088]可持久化最长不降子序列

    考虑二分求LIS的过程,就是维护一个序列,其中第i个数表示长度为i的最小结尾,而插入操作就是查找第一个大于x的位置并替换掉 用线段树维护,二分的过程也可以用线段树来完成,对线段树可持久化即可 1 #i ...

  7. Docker极简入门:使用Docker运行Java程序

    运行简单的Java程序 先在当前目录创建App.java文件 public class App{ public static void main(String[] args){ String os = ...

  8. springboot上传文件异常解决方案

    配置文件加入: #配置文件传输 spring.servlet.multipart.enabled =true spring.servlet.multipart.file-size-threshold ...

  9. linux结束进程命令

    在linux中,进程之间通过信号来通信.进程的信号就是预定义好一个消息,进程能识别它并决定忽略还是做出反应. 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL ...

  10. JavaWeb Cookie,Session

    Cookie 1.Cookie翻译过来是饼干的意思.Cookie是服务器通知客户端保存键值对的一种技术.客户端有了Cookie 后,每次请求都发送给服务器.每个Cookie的大小不能超过4kb. 2. ...