Re: speaking of C++...


Subject: Re: speaking of C++...
From: Joaquín Cuenca Abela (cuenca@celium.net)
Date: Mon Apr 16 2001 - 04:07:45 CDT


Vlad Harchev wrote:
>
> On Sun, 15 Apr 2001, Joaquín Cuenca Abela wrote:
>
> Hi,
>
> > ok, I've been debuggin ~2 hours to fix this one...
> >
> > NEVER call a virtual method from a constructor.
>
> > In the constructor of XAP_EncodingManager & XAP_UnixEncodingManager we
> > can found a single line:
> > { initialize(); }
> >
> > I don't know why g++ was so severely screwed to build a non-sense object
> > file (maybe is due to my version) instead of just build a version that
> > calls the base method, and I don't know why it worked before my changes,
>
> Do you mean that initialize() form derived class was being called by
> XAP_EncodingManager's ctor? If yes, it's a bug in your gcc. Mine is 2.95.3 and
> it works correctly.

mine usually works... I'm far more confident in its c++ skills than in
mines, but sometimes... :)

> > but it seems to me obvious that the author of this class wanted to call
> > the XAP_EncodingManager::initialize method from the XAP_EncodingManager
> > ctor and XAP_UnixEncodingManager::initialize from the
> > XAP_UnixEncodingManager ctor, BUT initialize actually is a VIRTUAL
> > method (and it took me ~2 hours fighting with the debugger to realize
> > it).
>
> C++ allows to call virtual methods from [cd]tros and and the method that
> actually will be called is the one defined in that class or in its parents

yes, I know. What socks me is that:
1) it worked before my changes
2) with the changes that I did, the debugger was puzzling me (of course
I was thinking that the ctor was calling
XAP_UnixEncondingManager::initialize, but even when I realized that
initialize is virtual the results were still puzzling me)

anyway I didn't followed until the end of the
XAP_EncodingManager::initialize stuff, so maybe is not gcc's fault, I
don't know.

> (see C++ spec, paragraph 12.7.3). But yes, sorry,
> XAP_UnixEncodingManager::initialize() shouldn't have called
> XAP_EncodingManager::initialize() explicitly - but granted, it's safe to call
> XAP_EncodingManager::initialize() arbitrary number of times.
>
> > So, please, try to avoid virtual methods calls from ctors and dtors.
>
> Still not obvious why.

you're right.
Maybe something:

* Try to avoid virtual methods calls from [cd]tors unless you know what
are you doing

(while legal, I will say that it's not intuitive behaviour. Maybe we
can add a link to the C++ Lite FAQ.)

Cheers,

--
Joaquín Cuenca Abela
cuenca@celium.net



This archive was generated by hypermail 2b25 : Mon Apr 16 2001 - 04:08:17 CDT