Dynamic _Cast in C++ (original) (raw)

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.

#include using namespace std;

class Base{ public:

// Required for dynamic_cast
virtual void show() { }

};

class Derived : public Base { public: void display() { cout << "Dynamic Cast Successful"; } };

int main() { Base* ptr = new Derived();

Derived* dptr = dynamic_cast<Derived*>(ptr);

if (dptr)
    dptr->display();

delete ptr;
return 0;

}

`

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

**Return Value

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

C++ `

#include using namespace std;

// Base Class declaration class Base { public: virtual void print() { cout << "Base" << endl; } };

// Derived1 class declaration class Derived1 : public Base { public: void print() { cout << "Derived1" << endl; } };

int main() { Derived1 d1;

// Base class pointer holding
// Derived1 Class object
Base* bp = &d1;

// Dynamic_casting
Derived1* dp2 = dynamic_cast<Derived1*>(bp);
if (dp2 == nullptr)
    cout << "Casting Failed" << endl;
else
    cout << "Casting Successful" << endl;

return 0;

}

`

**Explanation:

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

C++ `

#include using namespace std;

// Non-polymorphic base class class Base { public: void print() { cout << "Base" << endl; } };

class Derived1 : public Base { public: void print() { cout << "Derived1" << endl; } };

int main() { Derived1 d1; Base* bp = &d1;

// Dynamic_casting
Derived1* dp2 = dynamic_cast<Derived1*>(bp);
if (dp2 == nullptr)
    cout << "Casting Failed" << endl;
else
    cout << "Casting Successful" << endl;

return 0;

}

`

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

Invalid Downcasting

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

C++ `

#include using namespace std;

class Base { virtual void print() { cout << "Base" << endl; } };

// Derived1 class declaration class Derived1 : public Base { void print() { cout << "Derived1" << endl; } };

// Derived2 class declaration class Derived2 : public Base { void print() { cout << "Derived2" << endl; } };

int main() { Derived1 d1; Base* bp = &d1;

// Dynamic Casting
Derived2* dp2 = dynamic_cast<Derived2*>(bp);
if (dp2 == nullptr)
    cout << "Casting Failed" << endl;
else
    cout << "Casting Successful" << endl;

return 0;

}

`

**Explanation

Downcasting References

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

C++ `

#include #include using namespace std;

class Base { virtual void print() { cout << "Base" << endl; } };

class Derived1 : public Base { void print() { cout << "Derived1" << endl; } };

class Derived2 : public Base { void print() { cout << "Derived2" << endl; } };

int main() { Derived1 d1; Base* bp = dynamic_cast<Base*>(&d1);

// Type casting
Derived1* dp2 = dynamic_cast<Derived1*>(bp);

// Exception handling block
try {
    Derived2& r1 = dynamic_cast<Derived2&>(d1);
}
catch (std::exception& e) {
    cout << e.what() << endl;
}

return 0;

}

`

**Output

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

**Explanation