Types:
Type casting refers to converting a variable from one type to another.
int i = 10;
double d = i; // int automatically promoted to double
Manual casting using casting operators.
C++ provides four explicit casting operators:
| Cast Type | Purpose | Syntax |
|---|---|---|
static_cast |
Safe compile-time conversions | static_cast<NewType>(value) |
dynamic_cast |
Safe downcasting in polymorphic types | dynamic_cast<NewType*>(ptr) |
const_cast |
Add/remove const qualifier |
const_cast<NewType>(value) |
reinterpret_cast |
Reinterpret memory bits (low-level) | reinterpret_cast<NewType>(value) |