Please consider the following code:
class foo
{
public:
foo(){}
~foo(){}
void done() { delete this;}
private:
int x;
};
What is
Both would cause an error, what you want is:
void main()
{
foo* a = new foo();
a->done();
}
Which the compiler will expand to something like below, which I hope makes deleting "this" a bit less confusing.
void __foo_done(foo* this)
{
delete this;
}
void main()
{
foo* a = new foo();
__foo_done(a);
}
See also, Is it legal (and moral) for a member function to say delete this?