Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
dynamic_cast is a type conversion operator in C++ used primarily for safe, runtime downcasting and cross-casting within a polymorphic class hierarchy. It leverages Run-Time Type Information (RTTI) to inspect the virtual method table (vtable) and verify the validity of the cast during program execution.
Syntax
new_type: Must be a pointer or a reference to a complete class type, or a pointer tovoid(void*).expression:- If
new_typeis a pointer,expressionmust be a pointer to a complete class type. - If
new_typeis an lvalue reference,expressionmust be an lvalue of a complete class type. - If
new_typeis an rvalue reference,expressionmust be a glvalue or a prvalue of a complete class type (where a prvalue undergoes temporary materialization).
- If
Technical Requirements
- Polymorphism: For downcasting (base to derived) or cross-casting (between sibling base classes in multiple inheritance), the source type of
expressionmust be polymorphic. This means the base class must declare or inherit at least onevirtualfunction (typically a virtual destructor). Attempting a runtimedynamic_caston a non-polymorphic type results in a compilation error. - RTTI: The compiler must have RTTI enabled to perform runtime type checks. If RTTI is disabled via compiler flags (e.g.,
-fno-rttiin GCC/Clang),dynamic_castwill fail to compile only when used for downcasting or cross-casting. Upcasting will still compile successfully without RTTI.
Execution Behavior and Failure Modes
The behavior ofdynamic_cast depends strictly on whether new_type is a pointer or a reference. The following examples assume this polymorphic class hierarchy:
1. Pointer Casting
When casting pointers,dynamic_cast checks if the dynamic type of the object pointed to by expression is a complete object of new_type (or derived from it).
- Success: Returns a valid pointer of type
new_type. - Failure: Returns a null pointer (
nullptr) of typenew_type. It does not throw an exception.
2. Reference Casting
Because C++ references cannot be null,dynamic_cast handles reference casting failures via the C++ exception handling mechanism.
- Success: Returns a valid reference of type
new_type. - Failure: Throws an exception of type
std::bad_cast.
3. Void Pointer Casting
Ifnew_type is void*, dynamic_cast determines the exact dynamic type of the object and returns a pointer to the most derived object (the complete object) pointed to by expression.
Compile-Time vs. Runtime Resolution
Whiledynamic_cast is known as a runtime cast, it does not always incur runtime overhead:
- Upcasting (Derived to Base): If
new_typeis an accessible, unambiguous base class of the type ofexpression,dynamic_castresolves entirely at compile-time. In this scenario, it behaves identically tostatic_cast, does not require the base class to be polymorphic, and does not require RTTI. - Downcasting / Cross-casting: Requires runtime resolution. The operator traverses the inheritance graph stored in the RTTI to determine if the target type is a valid cast for the current dynamic object.
Master C++ with Deep Grasping Methodology!Learn More





