This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C++: why implicit delete in destructor?


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]