Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 10

Type Casting in C++

In this lesson: All C++ casts — implicit, C-style, static_cast, dynamic_cast, const_cast, reinterpret_cast — with examples. In Hindi & English.

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:

C++
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:

C++
double d = 3.9;
int x = static_cast<int>(d);     // 3 (truncated)
cout << x << "\n";
Output:
3

dynamic_cast

For safe downcasting in a polymorphic hierarchy — checked at run time, returns null on failure:

C++
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:

C++
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:

C++
int n = 65;
int *p = &n;
long addr = reinterpret_cast<long>(p);   // pointer → integer

Which cast to use

CastUse forChecked
static_castNumeric, upcastsCompile time
dynamic_castSafe downcastsRun time
const_castAdd/remove constCompile time
reinterpret_castLow-level bit reinterpretationNone

Common mistakes

  • Using reinterpret_cast where static_cast is correct.
  • Modifying a truly const object after const_cast (undefined behaviour).
  • Forgetting to check dynamic_cast for null before using the pointer.
  • Relying on C-style casts that hide a dangerous conversion.
🏋️ Practice

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_cast for ordinary compile-time conversions.
  • dynamic_cast for safe run-time downcasting (needs virtual).
  • const_cast adds/removes const; reinterpret_cast reinterprets bits.
  • Named casts are clearer and safer than C-style casts.

Frequently Asked Questions

What is type casting in C++?
Type casting is converting a value from one type to another, such as turning a 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?
The named casts make your intent explicit and let the compiler enforce limits — 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.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicConstants and Literals in C++ Your progress: 10 / 81 (12%) · Lesson 11 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free