12.2. The Dynamic Cast Operator
C++ provides a special cast operator that performs type conversions at run time. The dynamic_cast operator converts base pointers and references to derived pointers and references, respectively (this is called downcasting). Whereas static_cast performs type conversions at compile time, dynamic_cast downcasts types at run time; hence, the word dynamic in its name. Dynamic casts are interesting because you can determine when they fail. Failed dynamic casts return 0 with pointers and throw exceptions with references.
Dynamic casts do not cast away const or volatile, nor do they convert one type to another unrelated type (const_cast and reinterpret_cast perform these types of conversions). Dynamic casts apply only ...
Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.