This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: c++/6006: problem with pure virtual functions and destructord
- From: Nathan Sidwell <nathan at acm dot org>
- To: jody at ifi dot unizh dot ch
- Cc: lerdsuwa at gcc dot gnu dot org, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, nobody at gcc dot gnu dot org, gcc-gnats at gcc dot gnu dot org
- Date: Wed, 15 May 2002 11:06:07 +0100
- Subject: Re: c++/6006: problem with pure virtual functions and destructord
- References: <20020514154623.21750.qmail@sources.redhat.com> <1021452117.3ce21f551e8fa@neutrino.ifi.unizh.ch>
- Reply-to: nathan at compsci dot bristol dot ac dot uk
jody@ifi.unizh.ch wrote:
>
> Hi
> Is there a particular reason why a a virtual function called
> in a destructor is the function of this class and not the derived class?
because the standard says so.
> (There are implementations of C++ where the virtual
> function of the derived class is called)
those implementations are buggy.
> In my example, however, the function g() calls the pure virtual
> function f(). But g() can be called from the destructor of A,
> without causing a problem.
neither call 'works'. Drat, someone's borrowed my c++ std, so this is
from memory. in the dtor of A, the B object is not complete (B's
dtor has been executed, modulo the bits pertaining to its bases).
We shouldn't call (non-static) member functions of B, because
there's no constructed B object for their this pointer to point to.
The std says, that in this case, virtual functions dispatch to the
function seen in the definition of the base being ctor'd or dtor'd
(i.e. what the final overrider would be if that base was the complete
object).
IIRC, calling a pure virtual function is undefined, so anything can
happen. g++ 3.0 reports
6006.cc:37: abstract virtual `virtual void A::f()' called from destructor
because it can prove that that resolved to a pure virtual. It cannot
do so for the one embedded in g, but if you executed such a program
it would fail.
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
The voices in my head told me to say this
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk