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. 理解ASP.NET Core - 日志(Logging)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 快速上手 添加日志提供程序 在文章主机(Host)中,讲到Host.CreateDefault ...

  2. 大爽Python入门教程 1-2 数与字符串

    大爽Python入门公开课教案 点击查看教程总目录 1 整数与浮点数 整数大家都知道,比如1, 2, 10, 123, 都是整数int. 浮点数是什么呢? 上一节的除法运算,不知道有没有人注意到,其结 ...

  3. Python 练习 进程

    ... """ 创建2个进程, 每一个进程都打出当前的时间和进程的标识,持续打十次. """ import multiprocessing ...

  4. php 图像和水印

    生成图像 $img = imagecreate(400,400); imagecolorallocate($img,255,255,255); imageellipse($img,200,200,50 ...

  5. 在CentOS(Linux)下用TomCat部署完java项目后,在Windows下可以访问8080,但无法通过输入页面名.jsp进入页面

    错误描述:今天第一次在linux下部署项目,写了个测试的项目,在CentOS下放行8080端口后,在Windows下可以访问8080,出现TomCat的欢迎页面,但想要进入某一个静态的jsp页面显示找 ...

  6. <C#任务导引教程>练习八

    //66,静态成员与非静态成员的访问方式using System;class Text{    public int x;    public static int y;//静态数据成员    voi ...

  7. IDEA生成doc文档-生成chm文档

    首先,打开IDEA,并找到Tools -> Generate JavaDoc- 可供查询的chm比那些HTML页面好看多了. 如果您用过JDK API的chm文档,那么您一定不会拒绝接受其它第三 ...

  8. banner.txt

    Spring Boot Version: ${spring-boot.version} __----~~~~~~~~~~~------___ . . ~~//====...... __--~ ~~ - ...

  9. LoadRunner 2020 社区版本负载机(Load generator)Linux 安装教程

    1.HP官方注册 下载 LoadRunner_2020_Edition_Standalone_Applications_Micro_Focus_LoadRunner_2020_Community_Ed ...

  10. Go IF 条件语句

    条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句. 以下是在大多数编程语言中发现的典型条件语句的一般形式 ...