Object Initialization
The process of specifying an initial value for an object is called initialization.
1. Types of initialization:
-
int adefault initialization: no initializer -
Traditional forms:
int a = 5copy initialization- inherited from C, used to not be efficient for complex objects
int a (5)direct initialization- introduced to be more efficient for complex objects
-
Modern forms: (RECOMMENDED)
int a { 5 }direct list initialization- Prior to C++11 there were a lot of special requirements for different initializations, list initialization was made to work in all situations and behave consistently;
- It's called list initialization because it provides a way to initialize objects with a list of values rather than a single one;
- Disallows narrowing conversions, gives a warning or an error.
int a {}value initialization (or zero initialization)- initializes the variables to zero or whatever is closest to it (zero initialization). For objects, it could initialize to predefined values (value initialization).
- and some others... #stub
Sometimes we have a lot of reused variables that sometimes don't get used. in such cases we can add [[maybe_unused]].
1.2. Performance
When initializing a shit ton of variables, sometimes it can be useful to use default initialization (that leaves an undefined variable).