Topic: virtual delete operator


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 21 Dec 1994 02:28:39 GMT
Raw View
swf@elsegundoca.ncr.com (Stan Friesen) writes:

>In article <D12J6I.1yF@applicom.co.il>, eyala@applicom.co.il (Eyal Allalouf) writes:
>|> Why not allow the delete operator to be virtual?

>This can already all be done - by making the *destructor* virtual!
>In effect, when the destructor is virtual, so is the operator delete.

No. Suppose we have

 class base {
  ... // add enough stuff to make this reasonable
  virtual ~base();
  operator delete(void*);
 };
 class der : public base {
  ...
  virtual ~der();
  operator delete(void*);
 };

 base* p = new der;
 delete p;

What happens? Because the dtor is virtual, der::~der() is called,
as we would wish. But because the class-specific operator delete()
is not virtual, we get base::operator delete(), not the der version.
--
Steve Clamage, stephen.clamage@eng.sun.com




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 21 Dec 1994 07:34:48 GMT
Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:

>swf@elsegundoca.ncr.com (Stan Friesen) writes:
>
>>eyala@applicom.co.il (Eyal Allalouf) writes:
>>|> Why not allow the delete operator to be virtual?
>
>>This can already all be done - by making the *destructor* virtual!
>>In effect, when the destructor is virtual, so is the operator delete.
>
>No. Suppose we have
>
> class base {
>  ... // add enough stuff to make this reasonable
>  virtual ~base();
>  operator delete(void*);
> };
> class der : public base {
>  ...
>  virtual ~der();
>  operator delete(void*);
> };
>
> base* p = new der;
> delete p;
>
>What happens? Because the dtor is virtual, der::~der() is called,
>as we would wish. But because the class-specific operator delete()
>is not virtual, we get base::operator delete(), not the der version.

No, you don't, you get der::operator delete().
See ARM 12.5, or try the example on a compiler or two.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: eyala@applicom.co.il (Eyal Allalouf)
Date: Mon, 19 Dec 1994 17:27:53 GMT
Raw View
Why not allow the delete operator to be virtual?
In many ways it is parallel to the constructor/destructor case where the
constructor is not virtual (as new is not) but the destructor IS virtual.
A virtual delete operator would allow adding overloading of memory management
(i.e. for debugging purposes), as it would allow to overload operator new
for some derived class, without worrying that it would be deleted while it
is syntactically recognized as its base class.

For instance:

/* Beginning of example. */

class A {
public:
 void*           operator new (size_t s)    { return ::operator new (s); }
 virtual void    operator delete (void* p)  { ::operator delete (p); }
};

class B : public A {
public:
 void*           operator new (size_t) // Something else...
 virtual void    operator delete (void*) // Something else...
};

void   delete_me (A* x)
{
 delete x;
}

/* End of example */

would work regardless of the run time type of x.

Please email me directly your answer as well as posting it, because I always
receive the news with several days delay.

-----------------------------------------------------------------------------
    Eyal Alaluf                                eyala@applicom.co.il
    Mainsoft Israel Ltd.                       c/o Applicom Systems Ltd.
                                               16 Abba Hillel Silver St.
    Phone : 972 -(3) 575 5550 ext : 1287       Ramat-Gan, 52506
    Fax   : 972 -(3) 751 5906                  Israel
------------------------------------------------------------------------------




Author: swf@elsegundoca.ncr.com (Stan Friesen)
Date: Tue, 20 Dec 1994 17:42:22 GMT
Raw View
In article <D12J6I.1yF@applicom.co.il>, eyala@applicom.co.il (Eyal Allalouf) writes:
|> Why not allow the delete operator to be virtual?
|> In many ways it is parallel to the constructor/destructor case where the
|> constructor is not virtual (as new is not) but the destructor IS virtual.
|> A virtual delete operator would allow adding overloading of memory management
|> (i.e. for debugging purposes), as it would allow to overload operator new
|> for some derived class, without worrying that it would be deleted while it
|> is syntactically recognized as its base class.

This can already all be done - by making the *destructor* virtual!
In effect, when the destructor is virtual, so is the operator delete.

--
swf@elsegundoca.attgis.com  sarima@netcom.com

The peace of God be with you.