Course Content
C++ Data Types
3. Other Data Types and Type Convesion
C++ Data Types
Explicit Type Conversion
There are several ways to explicitly perform type conversion in C++.
Let's start with C-style conversion:
Here is an example:
main.cpp
One more way is a function-style conversion:
For example:
main.cpp
There are also 4 type casting operators:
static_cast
- performs basic conversionsdynamic_cast
- used for more complex type conversions, particularly when dealing with inheritance and polymorphismreinterpret_cast
- performs low-level reinterpretation of typesconst_cast
- adds or removesconst
Most of the time, you only need a static_conversion
. We will not discuss other casts in-depth. Here is the syntax:
Here is an example:
main.cpp
We will not cover the difference between all of those. In general, static_cast
is preferable since it is more restrictive and can prevent some unexpected behavior with complex data types.
Select all the correct ways of performing type conversion.
Select a few correct answers
Everything was clear?
Section 3. Chapter 6