Initialization
In a programming context, learned first in CS138 about OOP. This is whenever you use the {}
syntax.
As you’ll see in the slides below, this way, you don’t do a Copy Assignment Operator, but rather directly call the default Constructor when you initialize an object inside the Constructor function.
This can get SUPER confusing with the edge cases.
To not use your mind, just use {}
.
Some resources
- https://blog.tartanllama.xyz/initialization-is-bonkers/
- https://akrzemi1.wordpress.com/2013/09/10/value-initialization-with-c/
C++: Initializer
Types of Initialization
- Zero Initialization
- Value Initialization
- Default Initialization
If no constructor is provided, the compiler will build it for you. For all class member variables not created, it will do the default initialization (allocate the memory space).
We have two types:
- Primitive
- Class
DANGER: Primitive types are NOT always set to 0. In the case of default initialization:
- I was doing CS247 when I realized that I was wrong about this. The primitive type has garbage value
The initialization sort of has this recursive definition.
Default Initialization
https://en.cppreference.com/w/cpp/language/default_initialization This is the initialization performed when an object is constructed with no initializer.
This simply allocates the memory space. The values will be garbage (some compilers initialize them for optimization..?).
Rule of thumb for default-initialization:
- Built-in types (int, char, float, pointers, etc.) will be left uninitialized, resulting in indeterminate values
- Class types will be default-initialized
Example:
Value Initialization
https://en.cppreference.com/w/cpp/language/value_initialization
This is the initialization performed when an object is constructed with an empty initializer {}
.
Rule of thumb for value initialization:
- Built-in types will be initialized to zero, or null for pointers
- Class types will be default-initialized (NOT value-initialized)
Example:
Warning
A certain type of initialization does not mean that everything inside the class is initialized the same way.
A class might be value initialized, but member fields are default initialized unless explicitly value intialized.
Consider the example below:
To fix, you can do something like
So you guarantee m
is always value initialized when Foo
is constructed:
- Notice that even though
foo
is default initialized,Member m
is value initialized
Zero Initialization
https://en.cppreference.com/w/cpp/language/zero_initialization
This is confusing, I don’t quite get the difference with value initialization. Does not have a dedicated syntax in the language.
Rule of thumb for zero initialization:
- For built-in types, zero initialization sets the variable to zero (or null for pointers).
- For class types, zero initialization sets each non-static member to zero.
Unfortunately, there’s no specific syntax for zero-initialization. It typically occurs in specific situations, such as when a global or static variable is declared and not explicitly initialized.
Example:
Example
Example taken from CS138