Topic: is delete polymorphic or not?!


Author: Jerry Coffin <jcoffin@taeus.com>
Date: 2000/08/11
Raw View
In article <8mpbnm$mgo$1@reader1.imaginet.fr>, hicham@citeweb.net
says...
> Hi,
> Consider the following code:
> class A {};
> class B : public virtual A {};
> class C : public A {};
> int main ()
> {
>     A* a1 = new C();
>     delete a1;        /*SEEMS to works fine*/
>     A* a2 = new B();
>     delete a2;        /*broke at run-time*/
>     return 0;
> }

If an object is every going to be deleted via a pointer to the base
class, the dtor MUST be virtual.

Note that in most cases making a function non-virtual instead of
virtual results in undesired, but well-defined behavior.  In the case
of destroying a derived object via a pointer to a base object, this
is NOT the case -- if you try to do this, you get undefined behavior.

At least 99% of the time, if a class is intended to be used
polymorphically, it should contain a virtual dtor.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Hicham BOUHMADI" <hicham@citeweb.net>
Date: 2000/08/09
Raw View
thanks for your answers.
Actually with virtual destructors it works fine.
I thought that the implicit destructor is virtual...


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/08/09
Raw View

Hicham BOUHMADI wrote:
>
> thanks for your answers.
> Actually with virtual destructors it works fine.
> I thought that the implicit destructor is virtual...
>
No it can't be.  If it were, then a class with the implicit destructor and
no other virtual functions would cause the class to become polymorhphic (with
the attendent space concerns).

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/10
Raw View
Ron Natalie wrote:
>
> Hicham BOUHMADI wrote:
> >
> > thanks for your answers.
> > Actually with virtual destructors it works fine.
> > I thought that the implicit destructor is virtual...
> >
> No it can't be.  If it were, then a class with the implicit destructor and
> no other virtual functions would cause the class to become polymorhphic (with
> the attendent space concerns).

Which would, incidentally, destroy backward compatibility with C.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Hicham BOUHMADI" <hicham@citeweb.net>
Date: 2000/08/08
Raw View
Hi,
Consider the following code:
class A {};
class B : public virtual A {};
class C : public A {};
int main ()
{
    A* a1 = new C();
    delete a1;        /*SEEMS to works fine*/
    A* a2 = new B();
    delete a2;        /*broke at run-time*/
    return 0;
}

The fact that B inherits virtualy from A makes an indirection in the C++
internal model. When I cast, new B() to A*, the two pointers are not equal.
delete a2 does not find the heap block... so it brokes.
With C, delete finds the heap block and seems to works fine. ('seems'
because in computer science I am not sure of too much things :-)

Is this a normal / standard behavior? or should I revise my C++ books?
If it is a standard behavior than I'll be disappointed, because this means
that delete is not polymorphic!!!

Thanks for any help...


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/08/08
Raw View
Hicham BOUHMADI wrote:
>
> class A {};
> class B : public virtual A {};
> class C : public A {};
> int main ()
> {
>     A* a1 = new C();
>     delete a1;        /*SEEMS to works fine*/
>     A* a2 = new B();
>     delete a2;        /*broke at run-time*/
>     return 0;
> }
>

>From the standard, section 5.3.5 "Delete", paragraph 3:

"In the first alternative (delete object), if the static type of the
operand is different from its dynamic type, the static type shall be a
base class of the operand's dynamic type and the static type shall have
a virtual destructor or the behavior is undefined. In the second
alternative (delete array) if the dynamic type of the object to be
deleted differs from its static type, the behavior is undefined.

You need to add a virtual destructor to the base class.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/08/09
Raw View

Hicham BOUHMADI wrote:
>
> Is this a normal / standard behavior? or should I revise my C++ books?
> If it is a standard behavior than I'll be disappointed, because this means
> that delete is not polymorphic!!!
>
Both examples are undefined behavior.  The standard requires A to have a virtual
destructor in both cases.



> The fact that B inherits virtualy from A makes an indirection in the C++
> internal model. When I cast, new B() to A*, the two pointers are not equal.

There is no reason that the pointers have to be equal.  C++ knows how to
convert them when you follow the rules.

> delete a2 does not find the heap block...

I'm not sure what you mean by this and how you've determined it, but if A::~A() is
declared virtual, then the right address should be calculated prior to the appropriate
operator delete is called.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk)
Date: 2000/08/09
Raw View
Tue,  8 Aug 2000 16:54:53 GMT, Hicham BOUHMADI <hicham@citeweb.net> pisze=
:

> If it is a standard behavior than I'll be disappointed, because
> this means that delete is not polymorphic!!!

It is polymorphic if and only if you declared the destructor of the
base class as virtual.

--=20
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZAST=CAPCZA
QRCZAK

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Hicham BOUHMADI" <hicham@citeweb.net>
Date: 2000/08/09
Raw View
thanks guys for your answers.
I have to be more carefull :-)



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]