C++: why implicit delete in destructor?
Fergus Henderson
fjh@cs.mu.oz.au
Mon Apr 30 11:28:00 GMT 2001
On 29-Apr-2001, Olaf Dietsche <olaf.dietsche--list.gcc-patches@exmail.de> wrote:
> why is there a call to (__builtin_)delete in every destructor?
That's the way Cfront did it...
> If I
> don't use the heap, there is no point in having this call in every
> destructor, even if it's not called.
>
> Below is a patch, which moves the call to delete out of the destructor
> to where the delete is actually made.
>
> Comments anybody? Did I miss something?
Does this patch handle the case of classes with overloaded operator delete
and virtual destructors?
#include <iostream.h>
struct Base {
virtual ~Base() { cout << "~Base()" << endl; }
void operator delete (void *)
{ cout << "Base::operator delete()" << endl; }
};
struct Derived : Base {
virtual ~Derived() { cout << "~Derived()" << endl; }
void operator delete (void *)
{ cout << "Derived::operator delete()" << endl; }
};
int main() {
Base *p = new Derived;
delete p;
return 0;
}
This example should output
~Derived()
~Base()
Derived::operator delete()
In other words, operator delete() acts as it if was virtual iff the
destructor is declared virtual.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: < http://www.cs.mu.oz.au/~fjh > | -- the last words of T. S. Garp.
More information about the Gcc
mailing list