- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
Possible Duplicate:
Is it safe todelete this
?
我一直在做一些工作,该类旨在充当链表中的节点,我想我应该为该类提供自己的删除功能,而不是由管理类执行。所以基本上是这样的:
void Class::Delete() {
//Some cleanup code before deleting the object
delete this;
}
现在我已经对此进行了测试,它似乎工作正常,但我过去遇到过一个问题,即对象一直处于运行代码的中间,被删除,然后显然通过尝试使用不再存在的对象。
由于“删除这个”就在函数的末尾,它显然退出了函数并且工作正常,但是这种做法是一个坏主意吗?如果我不小心,这会在我脸上炸开吗?
最佳答案
FAQlite很好地回答了这个问题:
As long as you're careful, it's OK for an object to commit suicide (delete this).
Here's how I define "careful":
- You must be absolutely 100% positive sure that this object was allocated via new (not by new[], nor by placement new, nor a local object on the stack, nor a global, nor a member of another object; but by plain ordinary new).
- You must be absolutely 100% positive sure that your member function will be the last member function invoked on this object.
- You must be absolutely 100% positive sure that the rest of your member function (after the delete this line) doesn't touch any piece of this object (including calling any other member functions or touching any data members).
- You must be absolutely 100% positive sure that no one even touches the this pointer itself after the delete this line. In other words, you must not examine it, compare it with another pointer, compare it with NULL, 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
任何其他指针一样小心。但是,与显式声明的指针相比,成员函数自杀可能会出错的地方更多。
关于c++ - "delete this"是个坏主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6339386/
如果一种编程语言允许创建数据库表,但只允许将一个字段用作索引……那会有多大限制?我不是真正的数据库程序员,所以我想知道这样的系统是否可以用作正式数据库的基础,或者只是一个玩具。 最佳答案 那将是极其有
希望高手能解惑。非常高的概述是我不是编码的初学者,但对 OOP 还是新手。这组消息类是我们正在编写的一个大型模拟应用程序的核心,我不想愚蠢地这样做——这个接口(interface)将应用程序切成两半,
我将我的应用程序的资源保存在一个单独的 DLL 中,并在我的主 EXE 中使用 App.xaml 中的类似内容引用它们:- 当我编辑主 EXE 项目中的窗口时,VS2010 设计器无法识
我是颠覆的新手。最近,我在两个不同的分支中进行了一些开发,其中一个分支是另一个分支的分支。我已经将一些更改从第一个分支合并到主干。然而,当尝试将其他分支的更改合并到主干时,一切都变得困惑。也就是说,我
我正在查看不久前编写的一些代码,这让我非常紧张。问题中方法的大致形状是这样的; public Task Foo(...){ SyncMethod(); SyncMethod(); .
这部分是风格问题,部分是正确性问题。提交以下示例(处理包含嵌入式 header 的数据 block 的类的精简版): class Foo { public: Foo(size_t size)
说吧。如果我在我的系统上注册了 1,000,000 个用户,并且我有一个包含 500 个名称的列表。 我想将这些名称与我系统上的用户数量进行交叉检查,以查看哪些名称/用户已在数据库中注册。 这样的过程
在 PHP 应用程序中,在对象的析构函数期间运行我的保存代码是不是一个坏主意?我之所以问,是因为如果没问题,那么我可以在父 Model 类的析构函数中添加一个 save() 调用,这样就省去了内存其他
我们的测试套件中需要一些全局一次性设置代码。我们可以多次这样做,但这需要相当长的时间。 所有灯具都需要它,因此[TestFixtureSetUp]不起作用。它必须在所有 [TestFixtureSet
对于我网站的后端,只有少数人可见,我有一个系统,通过 ajax 与 php 进行通信,如下所示: function ajax(url, opts) { var progress = false
我在互联网上闲逛了一下,缺乏发现让我猜测是的,但是使用数组来填充 JMenuBar 的项目不是一个好主意吗?我也认为是的,因为每当你想知道“这是一个坏主意吗?”通常是这样。然而,我对编程的新手让我怀疑
我正在开发一个应用程序,其中方向(向前/向后)的概念非常重要。 问题是在整个代码库中存在着几种不同的约定:在某些地方它是 true/false,而在其他地方则是 +1/-1。 为了尝试将它们组合在一起
在我们的一个项目中广泛使用的包含文件中,我们有这些方便的常量: const double kPi = asin(1.0) * 2.0; const double kPiHalf =
我刚刚意识到我可以定义自己的 Prelude 模块并仔细控制它的导出。 这被认为是不好的做法吗? 优点: 无需在大型项目中重复导入“通用”模块。 无需写“导入前奏隐藏(catch)”。 最佳答案 一般
我有一个实体,使用 DateTime 作为 Id 似乎很合适,但是我有一种奇怪的感觉,它可能是一个糟糕的想法(虽然我无法想出任何合乎逻辑的理由来说明为什么), 这是一个不可变的对象(即一旦记录了值,它
我正在尝试编写一个元素制作系统,我可能希望有一天将其放入游戏中。有 Recipe 指定他们需要的成分和他们生产的东西。 我希望食谱具有灵 active ,这样它们只需要种类繁多的配料,而不是一种确切的
现状 想象一下: 有一个这样的枚举: enum State{ INITIAL{ @Override public void proceed(){...} }, NEXT_S
常识是,仅仅因为您可以扩充原生类型,并不意味着您应该。 扩充原生类型原型(prototype)始终不是一个好主意,唯一的异常(exception)是 ECMAScript 规范中的 polyfill
我不是经验丰富的 iPhone 用户,所以我有一个关于在我的应用程序中处理纵向/横向布局的好方法的问题。 问题是我当前的 UI 设计方式不太适合横向布局/不可用。那么,这是 iPhone 应用程序强制
我正在使用一个类别将 +load 添加到 UIResponder,这样我放在一起的 Cocoapod 就可以得到一个 didFinishLaunching使用 Cocoapod 的人无需修改他们的 a
我是一名优秀的程序员,十分优秀!