Re: Polymorphism
Alamelu wrote:
Thanks for your clarification.
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)
2) Why does c++ allow static_cast for base to derived conversion, when
actually will this be needed?
Alamelu:
1. dynamic_cast is used for conversion from Base* to Dereived*. No cast
is necessary for converting Derived* to Base*.
dynamic_cast is never *needed*. You can always use static_cast if you
are *sure* of the type.
2. Not sure what your question is here. static_cast is generally
applicable where the conversion is (or might be) correct according to
the language. Converting Base* to Derived* is one such usage. Uses of
static_cast are portable.
OTOH, uses of reinterpret_cast are generally non-portable; for example
conversion of a pointer type to int.
--
David Wilkinson
Visual C++ MVP