Type Conversion and Type Casting
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is type casting?
Type casting converts a value from one type to another — a double to an int, a base pointer to a derived one. C++ gives four named cast operators that make each conversion explicit and safer than the old C-style (type)value form.
C-style casts
The old way still compiles but hides intent — it silently tries several conversions:
double d = 3.9; int x = (int)d; // C-style: works, but which cast is this?
Prefer the named casts below — they are clearer and searchable.
static_cast
The everyday cast: numeric conversions and upcasts, checked at compile time:
double d = 3.9; int x = static_cast<int>(d); // 3 (truncated) cout << x << "\n";
3
dynamic_cast
For safe downcasting in a polymorphic hierarchy — checked at run time, returns null on failure:
class Animal { public: virtual ~Animal() {} };
class Dog : public Animal { public: void bark() {} };
Animal *a = new Dog();
Dog *d = dynamic_cast<Dog*>(a); // succeeds → not null
if (d) d->bark();It needs a virtual function in the base to work.
const_cast
Adds or removes const — mainly to call old non-const-correct APIs:
void oldApi(char *s); // not const-correct const char *msg = "hi"; oldApi(const_cast<char*>(msg)); // only safe if oldApi doesn't modify
reinterpret_cast
The most dangerous — reinterprets raw bits as an unrelated type. For low-level work only:
int n = 65; int *p = &n; long addr = reinterpret_cast<long>(p); // pointer → integer
Which cast to use
| Cast | Use for | Checked |
|---|---|---|
static_cast | Numeric, upcasts | Compile time |
dynamic_cast | Safe downcasts | Run time |
const_cast | Add/remove const | Compile time |
reinterpret_cast | Low-level bit reinterpretation | None |
Common mistakes
- Using
reinterpret_castwherestatic_castis correct. - Modifying a truly
constobject afterconst_cast(undefined behaviour). - Forgetting to check
dynamic_castfor null before using the pointer. - Relying on C-style casts that hide a dangerous conversion.
Build an Animal base with Dog and Cat subclasses. Store them as Animal*, then use dynamic_cast to find which ones are dogs and call a dog-only method safely.
Summary
- Type casting converts between types; prefer C++ named casts.
static_castfor ordinary compile-time conversions.dynamic_castfor safe run-time downcasting (needs virtual).const_castadds/removes const;reinterpret_castreinterprets bits.- Named casts are clearer and safer than C-style casts.
Frequently Asked Questions
What is type casting in C++?
double into an int or a base pointer into a derived pointer. C++ offers four named cast operators — static_cast, dynamic_cast, const_cast and reinterpret_cast — that make the intent of each conversion explicit and safer than old C-style casts.What is the difference between static_cast and dynamic_cast in C++?
static_cast performs conversions checked at compile time, such as numeric conversions or upcasts, with no run-time cost. dynamic_cast is used for safe downcasting in an inheritance hierarchy and checks at run time whether the object is really of the target type, returning null (for pointers) if it is not.When should you use const_cast in C++?
const_cast adds or removes const (or volatile) from a type. Its main legitimate use is to call an older API that is not const-correct with data you know is safe to modify; casting away const and then actually modifying a truly const object is undefined behaviour.What does reinterpret_cast do in C++?
reinterpret_cast reinterprets the bit pattern of a value as a different, unrelated type, such as converting a pointer to an integer. It is the most dangerous cast, used for low-level work, and gives no guarantees about portability, so it should be avoided unless absolutely necessary.Why prefer C++ named casts over C-style casts?
static_cast will not silently strip const, for example. A C-style cast tries several conversions in turn and can hide dangerous ones, whereas named casts are searchable, clearer to readers, and safer.Type casting क्या है?
Type casting किसी value को एक type से दूसरे में बदलता है — double को int, base pointer को derived में। C++ चार नामित cast operators देता है जो हर conversion को स्पष्ट और पुराने C-style (type)value रूप से सुरक्षित बनाते हैं।
C-style casts
पुराना तरीका अब भी compile होता है पर इरादा छिपाता है — यह चुपचाप कई conversions आज़माता है:
double d = 3.9; int x = (int)d; // C-style: chalta hai, par yeh kaun-sa cast hai?
नीचे दिए नामित casts पसंद करें — वे साफ़ और खोजने योग्य हैं।
static_cast
रोज़मर्रा का cast: numeric conversions और upcasts, compile time पर जाँचे:
double d = 3.9; int x = static_cast<int>(d); // 3 (truncate) cout << x << "\n";
3
dynamic_cast
Polymorphic hierarchy में सुरक्षित downcasting को — run time पर जाँचा, विफलता पर null लौटाता:
class Animal { public: virtual ~Animal() {} };
class Dog : public Animal { public: void bark() {} };
Animal *a = new Dog();
Dog *d = dynamic_cast<Dog*>(a); // safal → null nahi
if (d) d->bark();काम करने को इसे base में एक virtual function चाहिए।
const_cast
const जोड़ता या हटाता है — मुख्यतः पुराने non-const-correct APIs call करने को:
void oldApi(char *s); // const-correct nahi const char *msg = "hi"; oldApi(const_cast<char*>(msg)); // sirf tab safe jab oldApi badle nahi
reinterpret_cast
सबसे खतरनाक — raw bits को असंबंधित type के रूप में फिर व्याख्या करता है। केवल low-level काम को:
int n = 65; int *p = &n; long addr = reinterpret_cast<long>(p); // pointer → integer
कौन-सा cast इस्तेमाल करें
| Cast | किसके लिए | जाँच |
|---|---|---|
static_cast | Numeric, upcasts | Compile time |
dynamic_cast | सुरक्षित downcasts | Run time |
const_cast | const जोड़ना/हटाना | Compile time |
reinterpret_cast | Low-level bit फिर-व्याख्या | कोई नहीं |
आम गलतियाँ
- जहाँ
static_castसही है वहाँreinterpret_castइस्तेमाल करना। const_castके बाद सचमुचconstobject बदलना (undefined behaviour)।- Pointer इस्तेमाल करने से पहले
dynamic_castको null जाँचना भूलना। - ऐसे C-style casts पर निर्भर रहना जो खतरनाक conversion छिपाएँ।
एक Animal base बनाएँ जिसमें Dog और Cat subclasses हों। इन्हें Animal* के रूप में रखें, फिर dynamic_cast से पता करें कौन-से dogs हैं और dog-only method सुरक्षित रूप से call करें।
सारांश
- Type casting types के बीच बदलता है; C++ नामित casts पसंद करें।
- सामान्य compile-time conversions को
static_cast। - सुरक्षित run-time downcasting को
dynamic_cast(virtual चाहिए)। const_castconst जोड़ता/हटाता;reinterpret_castbits फिर-व्याख्या करता।- नामित casts C-style casts से साफ़ और सुरक्षित हैं।