Re: speaking of C++...


Subject: Re: speaking of C++...
From: Vlad Harchev (hvv@hippo.ru)
Date: Mon Apr 16 2001 - 02:22:36 CDT


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.

> 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
(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.

> Cheers,
>
> --
> Joaquín Cuenca Abela (who has a heavy headache...)
> cuenca@celium.net
>

 Best regards,
  -Vlad



This archive was generated by hypermail 2b25 : Mon Apr 16 2001 - 02:37:26 CDT