[英]what will happen if you do “delete this;” in a member function?
What will exactly happen if a member function try to do delete this;
如果成员 function 尝试
delete this;
, like in the constructor of the following class? ,比如在下面的class的构造函数中?
class A
{
public:
A(int);
~A();
int *pi;
}
A::A(int i)
{
delete this;
pi = new int(i);
}
A::~A()
{
delete pi;
}
This C++ FAQ entry answers this quite nicely, repeating here:这个C++ FAQ 条目很好地回答了这个问题,在这里重复:
As long as you're careful, it's OK for an object to delete this
.只要你小心,object
delete this
.
Here's how I define "careful":以下是我对“小心”的定义:
You are violating the #3 by accessing pi
after delete this
delete this
后访问pi
违反了#3
Bad things.坏事。 You can
delete this;
你可以
delete this;
but it's generally speaking an extremely bad idea, and once it's done, you cannot touch any member variables or member functions.但这通常是一个非常糟糕的主意,一旦完成,您就无法触摸任何成员变量或成员函数。
In general, invoking delete this
from inside a member function is well-defined, if a little risky.一般来说,从成员 function 内部调用
delete this
是明确定义的,如果有一点风险的话。
But it's probably a very bad idea to invoke it from the constructor (I don't know if it's well-defined).但是从构造函数调用它可能是一个非常糟糕的主意(我不知道它是否定义明确)。 Why would you ever want to do that?
为什么你会想要那样做?
You can delete this
, but this assumes that the instance was created with new
and that you don't access any members of the instance any more.您可以
delete this
,但这假定该实例是使用new
创建的,并且您不再访问该实例的任何成员。
In your example, pretty much the same would happen if you did在您的示例中,如果您这样做,几乎会发生相同的情况
class A
{
public:
A(int);
int *pi;
};
A::A(int i)
{
pi = new int(i);
}
int main()
{
A* p = new A(10);
delete p;
p->pi = new int; //bad stuff
//or
A a(20);
delete &a; //bad stuff
a.pi = new int; //more bad stuff
}
And even more bad stuff happens because when you delete this
, the pi
member is unitialized, leading to destructor attempting to delete a dangling pointer.更糟糕的事情发生了,因为当你
delete this
时, pi
成员被统一化,导致析构函数试图删除一个悬空指针。
1) delete operator works only for objects allocated using new operator. 1) delete 运算符仅适用于使用 new 运算符分配的对象。 If the object is created using new, then we can do delete this, otherwise behavior is undefined.
如果 object 是使用 new 创建的,那么我们可以删除它,否则行为未定义。
class A {
public:
void fun(){
delete this;
}
};
int main(){
/* Following is Valid */
A *ptr = new A;
ptr->fun();
ptr = NULL // make ptr NULL to make sure that things are not accessed using ptr.
/* And following is Invalid: Undefined Behavior */
A a;
a.fun();
return 0;
}
2) Once delete this is done, any member of the deleted object should not be accessed after deletion. 2)一旦删除完成,删除后object的任何成员都不应访问。
class A {
int x;
public:
A() { x = 0;}
void fun() {
delete this;
/* Invalid: Undefined Behavior */
cout<<x;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.