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



Am Mit, 2002-09-18 um 16.29 schrieb Paulo Pinto:
> 
> ----- Original Message -----
> From: "Nick Huxley" <nick anvil co uk>
> To: "Murray Cumming" <murrayc usa net>
> Cc: "Paulo Pinto" <pjmlp progtools org>; "gtkmm-main" <gtkmm-list gnome org>
> Sent: Wednesday, September 18, 2002 12:23 PM
> Subject: Re: [gtkmm] Replacing dynamic_cast<> with static_cast<> - compiler
> error.
> 
> 
> > On 18 Sep 2002, Murray Cumming wrote:
> >
> > > On Wed, 2002-09-18 at 12:15, Paulo Pinto wrote:
> > > > Sorry for the naive question, but what is
> > > > the relation between `Glib::ObjectBase *'
> > > > and `Atk::Object *' ?
> > >
> > > http://www.gtkmm.org/gtkmm2/reference/html/classAtk_1_1Object.html
> > >
> > > It's a base class. Actually it's a virtual base class. I think I have
> > > learnt that static_cast<> can never be used to cast down from a virtual
> > > base class, so none of these dynamic_cast<>s can be changed to
> > > static_cast<>s.
> > >
> >
> > Unfortunately you are right. Since we're dealing with pointers for the
> > polymorphism then the compiler can't know what will be passed in so a
> > static_cast cannot be used. The dynamic_cast checks at run time and will
> > throw an exception if the cast cannot be meaningfully performed.
> >
> > Nick
> 
> 
> Right. I know that it doesn't sound pretty but we could replace the
> dynamic_cast
> with a reinterpret_cast or even an old style cast (ugh did I just said
> that?).

according to Stroustrup, reinterpret_cast<> is very evil and shouldn't
be used for that purpose.
const_cast<>: you cannot downcast, right. That's why there's
dynamic_cast<>

Greetings,
Christian




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