Features #
-
dynamic_cast
converts a pointer or reference of a class to a pointer or reference in the same inheritance hierarchy. -
It can only be used with polymorphic classes. With
dynamic_cast
, we cast up, down, and across the inheritance hierarchy. -
Type information at run time is used to determine if the cast is valid.
-
If the cast is not possible, we will get a
nullptr
in case of a pointer, and anstd::bad_cast-exception
in case of a reference. -
dynamic_cast
is mostly used when converting from a derived class to a base class, but can also work the opposite operation.
Example #
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy