1) C++ doesn't require the user to explicitily say dynamic_cast for
derived to base conversion. Is it users best pratise to say dynamic_cast
during such conversion?
Base *pB1 = new Base;
Derived *pD1 = new Derived;
pB1 = pD1; // Doesn't require explicite dynamic_cast
When is the user supposed to explicitily say dynamic_cast, something like
this Base *pB = dynamic_cast<Derived*>(&D)
Ahem, you're now talking about dynamic_cast, not reinterpret_cast, this is a
totally different beast!
Anyway: most conversions that happen implicitly are safe, which is probably
also the reason that they don't require an explicit conversion. Converting
a pointer-to-derived to a pointer-to-base is safe because of the ISA
relationship (Liskow substitution principle, IIRC), so it doesn't need a
In your example above, you are further converting a pointer-to-derived to a
pointer-to-derived and the compiler then performs an implicit conversion to
2) Why does c++ allow static_cast for base to derived conversion, when
actually will this be needed?
It isn't needed. The difference between a dynamic_cast and a static_cast is
that a dynamic_cast can fail. If the given object's dynamic type isn't the
required type, it will either return a null pointer or throw a
std::bad_cast (depending on the used syntax). A static_cast is performed
without checking. Further, a static_cast is performed without using RTTI,
which makes it the only thing available for objects that don't have RTTI,
i.e. when using it with a pointer-to-void or other types that don't have
any virtual functions.
"Ulrich Eckhardt" wrote:
Please don't quote unrelated content. If you want to refer to something in
the posting you are answering, use the quoted text to directly relate to
it. Otherwise, also consider starting a new thread for the new topic. This
is also known as Usenet netiquette, please read a bit about it.