Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2000-05-30 10:15:48

> One thing I'm looking for is a way to explicitly get a base class pointer
> from a derived class pointer without declaring a new variable and without
> doing something which might allow downcasts or other potential mistakes.
> would be great to do the same for references.
> boost::upcast<>() anyone?

template <typename Base, typename Derived>
Base * upcast(Derived * const d)
{ return static_cast<Derived *>(static_cast<Base *>(d)); }

Should work for casting up hierarchy, fail for casting down or across (with
an error message like "cannot convert 'Derived *' to 'Base *'"). Will allow
casting to the same type; a POSTULATE can easily be added to prevent this if
desired. More complicated tests not done :).


Boost list run by bdawes at, gregod at, cpdaniel at, john at