Type Conversion, Type Aliases and Type Deduction Flashcards
Implicit type conversion
is performed when one one data type is expected, but a different data type is supplied
If the compiler knows how to convert, it will. Otherwise you get a compile error
Name three standard conversions
numeric promotion, numeric conversions and arithmetic conversions
numeric promotion
is the conversion of smaller numeric types to larger numeric types e.g. int to double
includes integral and floating point promotions
are value preserving
value preserving
no loss of value or precision
numeric conversion
a type conversion between fundamental types that isn’t a numeric promotion
narrowing conversion
type of numeric conversion that may result in the loss of value or precision
usual arithmetic conversions
rules applied when operands of different types are provided to binary operators
e.g. 5.5 + 5; => 5.5 + 5.0;
explicit type conversion
performed when programmer explicitly requests conversion via a cast
2 types of casts you should do in c++
static casts and dynamic casts
static cast
it provides compile-time type checking and only allows conversions that are considered safe by the compiler
dynamic cast
Another type of casting operator used for casting pointers or references to classes up and down the class inheritance hierarchy.
It’s primarily used for performing safe downcasting (casting from a base class pointer or reference to a derived class pointer or reference) and for performing runtime type checking.
typedefs / type aliases
Show code example
allow programmer to create an alias for a data type
e.g. using Degrees = double;
auto keyword for type uses
can be used to do type deduction, this drops const and references