Topic: Q : dynamic_cast<>()


Author: "Dmitry M. Potapov" <dima@florin.msk.su>
Date: 1996/03/21
Raw View
When bad_casting to pointer type,
dynamic_cast<>() returns NULL.

Is there a standard on good_casting,
I mean is it normal if dynamic_cast changes pointer,
and could dynamic_cast<>() be implemented w/o changing
the pointer if the cast was not bad ?

Thanks in advance,

Potapov Dmitry, dima@florin.msk.su
--



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/03/22
Raw View
In article RAA26760@florin.msk.su, "Dmitry M. Potapov" <dima@florin.msk.su>
writes:
>When bad_casting to pointer type,
>dynamic_cast<>() returns NULL.
>
>Is there a standard on good_casting,
>I mean is it normal if dynamic_cast changes pointer,
>and could dynamic_cast<>() be implemented w/o changing
>the pointer if the cast was not bad ?

No cast changes a pointer. A cast yields a value based on an existing
pointer value. The new value might be the same or different, but the
original pointer is not changed.

Example, assuming each class has virtual functions:
 class B1 { int i; ... };
 class B2 { int j; ... }
 class D : B1, B2 { int k; ... };

Given a D*, you can cast the value to either a B1* or a B2*. One of
the results might be the same as the original, but they cannot both be
the same.

Similarly, given a B1* and a B2*, you can cast either value to a D*.
The result of one of the casts might be the same as the original value,
but they cannot both be the same as the original.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]