A
Alexander Stippler
Hi,
I have some question about the usage of delete. I have some reference
counted classes, all derived from SharedObject. It's destructor looks like
this:
~SharedObject()
{
if (--references_ == 0) {
delete this;
}
}
Now I'd like to know if
a) it's OK to use delete this (especially in the destructor)
and
b) my guess, about what happens, is correct.
In my understanding the destructor is entered and delete this is executed
(if "if" is entered of course). Delete, as far as I know, calls the
destructor of the object again, now the if-condition is false and the
storage is freed due to the first call of delete.
I may be absolutely wrong. I would not wonder. But what does really happen
and is it a good idea at all to use delete this.
regards,
alex
I have some question about the usage of delete. I have some reference
counted classes, all derived from SharedObject. It's destructor looks like
this:
~SharedObject()
{
if (--references_ == 0) {
delete this;
}
}
Now I'd like to know if
a) it's OK to use delete this (especially in the destructor)
and
b) my guess, about what happens, is correct.
In my understanding the destructor is entered and delete this is executed
(if "if" is entered of course). Delete, as far as I know, calls the
destructor of the object again, now the if-condition is false and the
storage is freed due to the first call of delete.
I may be absolutely wrong. I would not wonder. But what does really happen
and is it a good idea at all to use delete this.
regards,
alex