Key Points
Object Lifetime
- Every variable goes through the same 5 phases of object lifetime.
- The space for a local variable is allocated at the opening curly brace of the function where it's declared.
- The value of a variable is initialized on the line with its declaration.
- Once a variable's value is initialized, it is in use. For primitive values, that primarily means that its name is in scope, which means the name can be used in other expressions.
- The value of a variable is destroyed at the closing curly brace of the declaring block.
- Once a variable's value is destroyed, it is no longer in use. For primitive values, that primarily means that its name is out of scope, which means that the name cannot be used in other expressions.
- The space for a local variable is deallocated at the closing curly brace of the function where it's declared.
Numeric Types
- Numeric values in C++ can either be floating point (able to hold non-integer parts, like decimals) or integers (only able to hold integer values).
- Integer values in C++ can either be signed (able to hold positive and negative values) or unsigned (only able to hold non-negative values).
- Types like booleans and characters are numeric types in C++. A boolean, for example, is an integer type that is guaranteed to be able to hold two values.
Numeric Promotion and Conversion
- If a floating point is used where an integer type is needed, that is always conversion since the non-integer part of the value may be lost.
- If an integer type is used where a floating point is needed, that is always conversion since there may not be enough bits allocated to storing the integer part of the value.
- If a signed value is used where unsigned type is needed, that is always conversion since negative values cannot be correctly represented.
- If an unsigned value is used as a signed type of the same size is needed, that is always conversion since there may not be enough bits allocated to storing the magnitude of the value.
- If a value from a larger numeric type is used where a smaller numeric type is needed, that is always conversion since there may not be enough bits to store the value.
- If a value from a smaller numeric type is used as a larger numeric type of the same signedness is needed, that is always promotion since there will definitely be enough bits to store the value.
(When logged in, completion status appears here.)