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.
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.