Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Type casting is the process of converting one data type into another. C++ supports both implicit and explicit casting.
Done automatically by the compiler when there is no data loss.
int i = 10; double d = i; // 10.0
Manually specified by the programmer. Required when data loss may occur.
double d = 10.5; int i = (int)d; // C-style int j = static_cast<int>(d);
static_cast: Standard well-behaved casts.dynamic_cast: Safe downcasting in inheritance.const_cast: To add or remove const.reinterpret_cast: Low-level bitwise conversion (dangerous).Prefer static_cast over C-style casts (int)d because it's more restrictive, easier to find in code, and safer at compile-time.