O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required