This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: C++: why implicit delete in destructor?
- To: Olaf Dietsche <olaf dot dietsche--list dot gcc-patches at exmail dot de>
- Subject: Re: C++: why implicit delete in destructor?
- From: Fergus Henderson <fjh at cs dot mu dot oz dot au>
- Date: Tue, 1 May 2001 04:13:07 +1000
- Cc: gcc-patches at gcc dot gnu dot org, gcc at gcc dot gnu dot org
- References: <87n18zh0p2.fsf@tigram.bogus.local>
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.