Topic: explicitly destructing the base class
Author: jerry_jeremiah@yahoo.com ("Jerry Jeremiah")
Date: Fri, 25 May 2007 02:24:03 GMT Raw View
I came across something in some code where the derived class' destructor
was explicitly destructing the base class.
I think calling the base class=92 destructor from the derived class is
undefined: 12.4.12
And even if you could, after you call the base class=92 destructor from
the derived class then when the derived class=92 destructor finishes it
will implicitly call the base class=92 destructor (again) and that is
undefined: 12.4.14
Or am I wrong? The compiler the code is compiled with allows it...
Jerry
--=20
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: ron@spamcop.net (Ron Natalie)
Date: Fri, 25 May 2007 16:00:26 GMT Raw View
Jerry Jeremiah wrote:
> Or am I wrong? The compiler the code is compiled with allows it...
>
>
It's rarely necessary to call destructors. Usually doing so ends
up causing them to run twice on the same object (bad). The only
time it is of practical use is when using placement new. Since
the object is destroyed by delete, you'll have to kick off the
destructor yourself.
It would behoove you to have a virtual destructor if you are going
to do that on derived classes.
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Fri, 25 May 2007 16:15:57 GMT Raw View
Jerry Jeremiah ha scritto:
> I came across something in some code where the derived class' destructo=
r
> was explicitly destructing the base class.
I can't see any valid reason to write such code: the programmer should
be tied to a chair and forced to read the whole C++ standard again and
again until it has understood every line of it :-D
> I think calling the base class=E2=80=99 destructor from the derived cla=
ss is
> undefined: 12.4.12
I don't see how this paragraph applies.
> And even if you could, after you call the base class=E2=80=99 destructo=
r from
> the derived class then when the derived class=E2=80=99 destructor finis=
hes it
> will implicitly call the base class=E2=80=99 destructor (again) and tha=
t is
> undefined: 12.4.14
Correct. This paragraph applies and makes the behaviour undefined.
>=20
> Or am I wrong? The compiler the code is compiled with allows it...
>=20
Compilers are only required to diagnose ill-formed programs. Here we
have undefined behaviour, which is a completely different thing. See
clause 1.3 for the definition of these terms.
HTH,
Ganesh
---
[ 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://www.comeaucomputing.com/csc/faq.html ]