George Heintzelman writes:
>
> Hi,
>
> I was playing around a bit with dynamic_cast<>, having read a bit more
> in Stroustrup, who says:
>
> 'dynamic_cast<T*>(p) looks at the object pointed to by p (if any). If
> that object is of class T or has a unique base class of type T, then
> dynamic_cast returns a pointer of type T* to that object; otherwise, 0
> is returned.'
>
> This seems like very intuitive, useful, and meaningful behavior, and
> shouldn't be too hard to implement either, since you've already got
> dynamic type information running around.
>
> However, if in ROOT I do:
>
> class Base {
> int foo;
> };
>
> class Derived {
> int bar;
> };
>
> Derived *x = new Derived;
> Base *y = dynamic_cast<Base *>(x); // This works, as it should;
> TObject *z = dynamic_cast<TObject *>(x); // This gives non-zero,
> // as it shouldn't!
>
> George Heintzelman
> gah@bnl.gov
>
Hi - I'm not sure if CINT supports C++ RTTI at all and dynamic cast in
particular - may be Masa can comment on it. However ROOT provides an equivalent
of dynamic cast which is
Bool_t TObject::InheritsFrom(Text_t* classname)
At the same time I doubt that using the dynamic cast would help to solve
the problem you reported earlier today any better that "normal" C-style cast
would do. You defined an object which was both - Base and Derived -
simultaneously and asked the interpreter/compiler
to decide whether it was Base or Derived when the object was passed as
a parameter.... Looks like the perfect example of the situation where the
behaviour can be undefined.
best, Pasha
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:39 MET