Type Conversion and Casting in C++
C++ converts values implicitly during initialization, assignment, function calls, and expression evaluation, or explicitly through casts.
Summary
C++ converts values implicitly during initialization, assignment, function calls, and expression evaluation, or explicitly through casts. A cast expresses intent but does not guarantee information preservation or runtime safety. Named casts distinguish conversion mechanisms and make risky operations easier to audit.
| Named cast | Purpose | Important limitation |
|---|---|---|
| static_cast | Ordinary explicit conversions | Does not validate an object's dynamic type at runtime |
| dynamic_cast | Runtime-checked downcasts and cross-casts in polymorphic hierarchies | Failure returns nullptr for pointer casts or throws std::bad_cast for reference casts |
| const_cast | Changes const or volatile qualification | Modifying an object originally defined as const is undefined behavior |
| reinterpret_cast | Permitted low-level reinterpretations | Does not guarantee valid access through the destination type |
Narrowing can lose information. Brace initialization rejects narrowing, so int n{3.7}; is ill-formed. Floating-to-integer conversion truncates toward zero; behavior is undefined if the truncated value cannot fit in the destination integer type.
Conversion placement affects arithmetic. With integer operands and a nonzero divisor, static_cast<double>(a) / b performs floating-point division, whereas static_cast<double>(a / b) converts a quotient after integer division has already occurred. Unexpected implicit conversions can also cause surprising signed-unsigned comparisons.
An explicit constructor cannot supply an implicit conversion through copy-initialization, but direct initialization can invoke it. C-style casts can select among several conversion mechanisms, obscuring intent.
🧠 Key Concepts
- Implicit conversions
- Narrowing conversions
- static_cast
- dynamic_cast
- const_cast
- reinterpret_cast
- Integer division
- Floating-to-integer conversion
- Explicit constructors
- C-style casts
🧠 Quick Check
See what you remember from the summary.
In a polymorphic class hierarchy, what happens when a dynamic_cast to a pointer type fails?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Type Conversion and Casting in C++
📘 Overview C++ converts values between types either implicitly or through explicit casts. A cast makes conversion intent explicit, but it does not guarantee that the conversion preserves information or is safe at runtime.
🧠 Key Idea Choose conversions according to the relationship between the source and destination types. Prefer named casts because they distinguish ordinary conversions, runtime-checked class conversions, qualification changes, and low-level reinterpretation.
⚔️ Core Details: - Implicit conversions adapt values to required types during initialization, assignment, function calls, and expression evaluation. - Narrowing conversions can lose information when the destination type cannot represent the source value exactly. - static_cast<T>(value) requests a conversion checked at compile time without runtime validation of an object's dynamic type. - dynamic_cast<T>(value) checks downcasts and cross-casts at runtime for polymorphic class hierarchies; failure returns nullptr for pointer casts or throws std::bad_cast for reference casts. - const_cast<T>(value) changes const or volatile qualification; modifying an object originally defined as const causes undefined behavior. - reinterpret_cast<T>(value) performs permitted low-level reinterpretations; it does not establish that accessing the result through the destination type is valid.
🎯 Why It Matters: - Unexpected implicit conversions can introduce precision loss or surprising signed-unsigned comparisons. - The position of a conversion changes arithmetic behavior, especially when integer division occurs before conversion to floating point. - Named casts expose potentially risky operations during code review and make conversion intent easier to audit. - Runtime-checked casts can detect invalid class-hierarchy conversions that unchecked downcasts would miss.
🧠 Quick Recall: - List initialization - int n{3.7}; is ill-formed because brace initialization rejects narrowing conversions. - Integer division - For int operands a and b with nonzero b, static_cast<double>(a) / b performs floating-point division; static_cast<double>(a / b) converts an already-truncated quotient. - Floating-to-integer conversion - A representable result is truncated toward zero; conversion has undefined behavior if the truncated value cannot fit in the destination integer type. - explicit constructor - Prevents the constructor from being used for implicit conversions through copy-initialization; direct initialization can still invoke it. - C-style cast - The syntax (T)value can select among several conversion mechanisms, making its intent less precise than a named C++ cast.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.