Boost logo

Boost Users :

From: Jim.Hyslop (jim.hyslop_at_[hidden])
Date: 2002-06-27 08:06:50


Jean Llorca (yg-boost-users_at_[hidden]) wrote:
> I would like to propose, in the spirit of Bjarne Stroustrup book,
> an upcast (where target inherits source).
Upcasting is already directly supported by the language. Given classes Base
and Derived, with the public inheritance implied by their names, then:

   Derived d;
   Base * b=&d;
   Base & br = d;

will work without any casting whatsoever. When the compiler compiles this
code, it has the definitions of classes Base and Derived available, so all
the work can be performed at compile time.

> template <class Target, class Source>
> inline Target polymorphic_upcast(Source* x
> BOOST_EXPLICIT_DEFAULT_TARGET)
> {
>
> typedef unsigned int uint;
>
>
> uint offset = reinterpret_cast<uint>( static_cast<Source*>(
> reinterpret_cast<Target>(1) ) ) - 1;
> Target res = reinterpret_cast<Target>(
> reinterpret_cast<uint>( x ) -
> offset );
>
> assert( res == dynamic_cast<Target>(x) );
>
> return res;
> }
I don't understand this - how is casting a Target to an unsigned int going
to help? Did you perhaps mean Target * in the above example, or have I
missed something?

-- 
Jim

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