C++ 删除类内的self对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6952083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting self object inside class
提问by venkysmarty
Possible Duplicate:
C++: Delete this?
可能的重复:
C++:删除这个?
In C++, is it ok to delete the self object in function definition. What are side effects of this?
在 C++ 中,可以删除函数定义中的 self 对象。这有什么副作用?
class MyClass {
public:
void ~myClass() {}
void myFunction() {
// logic here
delete this;
}
}
Thanks!
谢谢!
回答by Zoey
You may delete an object from within itself, but it is necessary that you do not, afterward, access any member variables or functions of that class instance after doing so.
您可以从其内部删除一个对象,但之后必须不要访问该类实例的任何成员变量或函数。
回答by Nawaz
From parashift FAQ:
Is it legal (and moral) for a member function to say delete this?
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% positively 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% positively sure that your member function will be the last member function invoked on this object.
You must be absolutely 100% positively 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).
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.
成员函数说删除它是否合法(和道德)?
只要你小心,一个对象自杀是可以的(删除这个)。
以下是我如何定义“小心”:
你必须绝对 100% 肯定这个对象是通过 new 分配的(不是通过 new[],也不是通过放置 new,也不是堆栈上的局部对象,也不是全局对象,也不是另一个对象的成员;而是通过普通的普通新的)。
您必须绝对 100% 肯定您的成员函数将是在此对象上调用的最后一个成员函数。
您必须绝对 100% 肯定您的成员函数的其余部分(删除此行之后)不会触及此对象的任何部分(包括调用任何其他成员函数或触及任何数据成员)。
自然地,通常的警告适用于当您没有虚拟析构函数时您的 this 指针是指向基类的指针的情况。
回答by dash-tom-bang
The side effects of that are that the object is no longer valid, nor are pointers or references to that object.
这样做的副作用是对象不再有效,指向该对象的指针或引用也不再有效。
I've seen this pattern a lot of places. Typically it's used in a reference counting sort of situation, when the last reference to the object goes away the object deletes itself. It's also typically paired with a factory function of some sort, e.g. a static class member function named Create, taking no parameters, and returning a pointer to the class. The body of this function does the corresponding new
, and your constructor can even be private (that way people don't create the object in a way that will mess up your cleanup code).
我在很多地方都看到过这种模式。通常它用于引用计数类型的情况,当对对象的最后一个引用消失时,对象会删除自身。它通常还与某种工厂函数配对,例如名为 Create 的静态类成员函数,不带参数,并返回指向类的指针。此函数的主体执行相应的new
,并且您的构造函数甚至可以是私有的(这样人们就不会以一种会弄乱您的清理代码的方式创建对象)。
回答by Mat
It's pretty dangerous. Consider this:
这是相当危险的。考虑一下:
void foo() {
MyClass bar;
bar.myFunction(); // calls delete
} // bar goes out of scope, calls delete again
Check out this C++FAQ 16.15entry for when doing delete this
is possible - it's legal, just needs to be used bery carefully.
查看此C++FAQ 16.15条目以了解何时delete this
可以执行 - 这是合法的,只需要谨慎使用即可。
回答by Nikki Locke
Depends on your definition of ok!
取决于你对ok的定义!
You can do this, if you are careful, but you shouldn't do it without very good reason, because no-one will be expecting it, and because there is no guarantee that the object has been allocated with new.
如果你小心,你可以这样做,但你不应该没有很好的理由,因为没有人会期待它,因为不能保证对象已经被分配了 new。