C++ Programming/Programming Languages/C++/Code/Keywords/dynamic cast

The  keyword is used to casts a datum from one pointer or reference of a polymorphic type to another, similar to  but performing a type safety check at runtime to ensure the validity of the cast. Generally for the purpose of casting a pointer or reference up the inheritance chain (inheritance hierarchy) in a safe way, including performing so-called cross casts.


 * Syntax

The target type must be a pointer or reference type, and the expression must evaluate to a pointer or reference.

If you attempt to cast to a pointer type, and that type is not an actual type of the argument object, then the result of the cast will be.

If you attempt to cast to a reference type, and that type is not an actual type of the argument object, then the cast will throw a  exception.

When it doesn't fail, dynamic cast returns a pointer or reference of the target type to the object to which expression referred.