Skip to content

Type Conversions and Casting

Beginning of the lesson

Type Conversions and Casting

Turning one type into another - safely, deliberately, and only when you mean to

Big Idea: C++ is a strongly typed language, yet real programs constantly move values between types: an int into a double, a base pointer into a derived pointer, an API handle into an integer. Some of these conversions happen silently (the compiler inserts them for you), and some you must request explicitly with a cast. Knowing which conversions the compiler does behind your back, and which of the four named casts to reach for when you take control, is the difference between code that is provably correct and code that quietly corrupts data.
By the end you will be able to:
  • recognise implicit conversions and spot dangerous narrowing,
  • use the explicit keyword to stop unwanted conversions,
  • choose correctly between static_cast, dynamic_cast, const_cast and reinterpret_cast,
  • explain why the old C-style (T)x cast is discouraged in modern C++.

1. Implicit conversions and narrowing

An implicit conversion is one the compiler performs automatically, without you writing a cast. Many are harmless and useful: assigning an int to a double, or passing a char where an int is expected. These are promotions or widening conversions - the destination type can represent every value of the source type, so nothing is lost.

The danger is the opposite direction: a narrowing conversion, where the destination type cannot hold every value of the source. Assigning a double to an int throws away the fractional part; assigning a large int to a short may overflow into a meaningless value. The compiler usually allows this silently and at most emits a warning.

double pi = 3.14159;
int    n  = pi;        // narrowing: n becomes 3, the .14159 is lost
long   big = 100000;
short  s  = big;       // narrowing: may overflow, value is implementation-defined

Modern C++ gives you a safety net: brace initialisation {} turns a narrowing conversion into a compile error, not a silent surprise. This is one of the best reasons to prefer braces for initialisation.

int a = 3.9;     // compiles: a is 3 (silent narrowing)
int b{3.9};      // ERROR: narrowing conversion is not allowed inside { }

Implicit conversions also happen through converting constructors. If a class has a constructor taking a single argument, the compiler may use it to convert that argument type into your class silently - which is exactly what the next section learns to control.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers