VOOZH about

URL: https://www.geeksforgeeks.org/cpp/dynamic-_cast-in-cpp/

⇱ Dynamic _Cast in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic _Cast in C++

Last Updated : 15 Jun, 2026

dynamic_cast is a runtime type conversion operator used in polymorphic inheritance hierarchies. It performs runtime type checking to ensure that a conversion is valid before it is applied.

  • Commonly used for downcasting from a base class to a derived class.
  • Requires the base class to have at least one virtual function.
  • Indicates failed conversions through nullptr (for pointers) or std::bad_cast (for references).

Output
Dynamic Cast Successful

Explanation: dynamic_cast safely converts a base class pointer to a derived class pointer at runtime. If the conversion is valid, it returns the converted pointer; otherwise, it returns nullptr

Syntax

dynamic_cast<new_type>(expression)

Parameters

  • new_type: Target type for the conversion.
  • expression: Pointer or reference being converted.

Return Value

  • Returns a valid pointer if the cast succeeds.
  • Returns nullptr if a pointer cast fails.
  • Throws std::bad_cast if a reference cast fails.

Note: dynamic_cast relies on Run-Time Type Information (RTTI), which introduces some runtime overhead. If a conversion is guaranteed to be valid, static_cast is generally more efficient. 

Downcasting Using dynamic_cast

dynamic_cast is commonly used to safely convert a base class pointer to a derived class pointer.


Output
Casting Successful

Explanation:

  • bp is a base class pointer pointing to a Derived1 object.
  • dynamic_cast checks the actual object type at runtime.
  • Since the object is of type Derived1, the cast succeeds.
  • A valid Derived1* pointer is returned.

Requirement for dynamic_cast

For downcasting to work, the base class must be polymorphic. A class becomes polymorphic when it contains at least one virtual function.

Example: Non-Polymorphic Base Class


Output

main.cpp: In function ‘int main()’:
main.cpp:24:21: error: cannot ‘dynamic_cast’ ‘bp’ (of type ‘class Base*’) to type ‘class Derived1*’ (source type is not polymorphic)
24 | Derived1* dp2 = dynamic_cast<Derived1*>(bp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Explanation

  • The base class does not contain any virtual function.
  • Therefore, it is not polymorphic.
  • RTTI information is unavailable.
  • As a result, dynamic_cast cannot perform runtime type checking and compilation fails.

Invalid Downcasting

A cast fails when the actual object type does not match the target type.


Output
Casting Failed

Explanation

  • bp points to a Derived1 object.
  • The code attempts to convert it to Derived2*.
  • Since the object is not of type Derived2, the conversion is invalid.
  • dynamic_cast returns nullptr.

Downcasting References

When casting references, a failed conversion results in an exception instead of returning nullptr.

Output

main.cpp:31:15: warning: unused variable ‘dp2’ [-Wunused-variable]
31 | Derived1* dp2 = dynamic_cast<Derived1*>(bp);
| ^~~
std::bad_cast

Explanation

  • d1 is an object of type Derived1.
  • The code attempts to convert it to a Derived2 reference.
  • The conversion is invalid because the object is not a Derived2.
  • Since references cannot be nullptr, dynamic_cast throws a std::bad_cast exception.
Comment
Article Tags: