Default member initializer is a C++11 feature that allows for declaring default values directly at the point of member variable declarations in a class. This capability is often confused with other ways of initializing data.
Early C++ did not allow initialization of members directly at declaration; values were only assigned in the constructor (in the body or initialization list). The introduction of default member initializers (C++11) enhanced readability and reduced the risk of undefined initialization errors.
If fields are not explicitly initialized, they contain "garbage" (undefined) values. Assignment within a constructor is less efficient compared to using an initialization list, and ignoring default member initializers complicates class expansion and new constructor creation.
Use default member initializers for simple values, and for complex cases (especially if dependent or non-standard values are needed) use constructor initialization lists.
Example code:
class Widget { int x = 42; // default member initializer std::string name = "default"; // default member initializer public: Widget() = default; // x=42, name="default" Widget(int xx) : x(xx), name("new") {}// x=xx, name="new" };
Key features:
Will the default member initializer apply if the member is initialized inside the constructor body but not in the initialization list?
Answer:
No. If it's not specified in the initialization list, the variable will first initialize with the default member initializer, and then assignment will occur in the constructor body, which is less efficient.
What is the order of initialization of class members with default member initializers in inheritance?
Answer:
Base class members are initialized first, then derived class members; for each member with a default member initializer, the constructor's initialization list is used first if specified, then the default member initializer, otherwise, it remains uninitialized (for POD). There is no "double initialization."
Can default member initializers be applied to static class members?
Answer:
No, static members cannot be initialized through default member initializers. They must be initialized outside the class or by using inline static in C++17.
Example:
struct S { static int a = 5; // Error! };
A class with a dynamic string that is forgotten to be initialized in some constructors. Later on access — undefined behavior.
Pros:
Cons:
All fields have default member initializers. Additional constructors explicitly initialize the necessary members through the initialization list as needed.
Pros:
Cons: