Re: [gtkmm] Replacing dynamic_cast<> with static_cast<> - compiler error.



> 
> I'm experimenting with changing the dynamic_cast<>s in the default
> signal handlers with static_cast<>s to avoid the
> dynamic-cast-during-constructor gcc2.9* bug.
> 
> But I'm getting this error:
> 
> Atk::Object_Class::children_changed_callback (AtkObject *, unsigned 
> int, void *)':
> object.cc:229: invalid static_cast from type `Glib::ObjectBase *' to 
> type `Atk::Object *'
> 
> I can't understand why. I think I remember it being something do with
> Multiple Inheritance and/or virtual base classes. Can someone explain it
> to me again? I have attached the patch that produces this error.

Dynamic casting is always required whereever multiple inheritance is 
used.  This is because with multiple inheritance there is no guarantee
that the relationship between one derived type and its base class and
another is the same.    

     A  ->  B
     A  ->  C

     A -> B,C -> D

But there is only one A in D so either B cast to A or C cast to B
must change.  Thus dynamic cast is required as the relationship is
not "static".


There is a work around the bug though (as gtkmm had this bug a
long time ago and it was solved there as well).  It gets broken
every so often when the inheritance tree changes.  Simply reversing
the order of the declarations on the MI often fixes the problem iff
only one of B or C makes the dynamic cast in the constructor.
If that doesn't work you will need to do some really nasty kludges 
like verifying that vtable is properly accessed prior to making the
cast or implement your own dynamic cast system.   I toyed with
both ideas but seeing that reversing the order fixed it I never bothered
with the others.

As always best of luck.  Hope it helps.

--Karl



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]