Boost logo

Boost Users :

From: Sam Partington (sam.partington_at_[hidden])
Date: 2002-06-27 09:54:31


Hi, I feel I have to jump on this one ;-)

Jean, I think you have this one backwards I'm afraid, take your example
(stripped down a bit)

> -----Original Message-----
> From: news [mailto:news_at_[hidden]]On Behalf Of Jean Llorca
> Sent: 27 June 2002 15:31
> To: boost-users_at_[hidden]
> Subject: [Boost-Users] Re: boost::polymorphic_upcast suggestion
>
>
> "Darin Adler" <darin_at_[hidden]> a écrit dans le message de news:
> B586F909-89D7-11D6-9349-0003935B80A2_at_bentspoon.com...
> >
> > On Thursday, June 27, 2002, at 06:38 AM, Jean Llorca wrote:
> >
> > > Here upcasting means casting a base class to a derived class, I know
> > > it may seem strange, bust boost offers a downcast
> > > which does the static_cast the language does implicitly already (check
> > > <boost/cast.hpp>).
> >
> > I think you've got it backwards. Downcasting, in Boost at least, means
> > casting a pointer to a base class part of an object to a pointer to a
> > derived class part of the same object. The polymorphic_downcast is
> > *not* doing a static_cast for a conversion the language already does
> > implicitly.
>
> template <class Target, class Source>
> inline Target polymorphic_downcast(Source* x
> BOOST_EXPLICIT_DEFAULT_TARGET)
> {
> assert( dynamic_cast<Target>(x) == x ); // detect logic error
> return static_cast<Target>(x);
> }
>
> Sorry about it, but it's here in the code. This is in the last version of
> the CVS. It does a static_cast. The compiler does static_cast
> implicitly in
> some cases.
> For instance a C++ compiler replaces the assert line with:
> assert( dynamic_cast<Target>(x) == static_cast<Target>(x) );
>
> >
> > Could you give a specific complete example (a file that would compile,
> > with all the declarations) of how one would use what you're proposing
> > to help make this clearer?
>
> I enclosed two files to this reply. Don't scream about chosen class names
> and stuff in the example ;)
>
>
>
>
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net