- Does the compiler create a default constructor?
- Does the implicitly created default constructor create zero initialization?
If you legally understand the language of the 2003 standard, the answers are yes and no . However, this is not the whole story , because, unlike the default user constructor, the implicit default constructor is not always used when creating an object from scratch - there are two other scenarios: no construction and initialization of the element value.
The case with the lack of construction is just technicality, because it is functionally no different from calling the trivial default constructor. Another case is more interesting: member value initialization is initiated using "()" [as if explicitly calling a constructor that has no arguments], and it bypasses what is technically called the default constructor . Instead, it recursively initializes the values ​​for each data item, and for primitive data types, this ultimately allows zero initialization .
Thus, the compiler provides two different implicitly defined default constructors . One of which performs zero initialization of the data of a primitive element, and the other does not. Here are some examples of how you can call each type of constructor:
MyClass a; // default-construction or no construction MyClass b = MyClass(); // member-wise value-initialization
and
new MyClass; // default-construction or no construction new MyClass(); // member-wise value-initialization
Note. If there is a default constructor declared by the user , then the initialization of values ​​by elements simply calls this and stops.
Here are some detailed breakdowns of what the standard says about it ...
If you do not declare a constructor, the compiler implicitly creates a default constructor [12.1-5]
The default constructor does not initialize primitive types [12.1-7]
MyClass() {}
If you initialize the object with "()", this does not directly call the default constructor. Instead, it initiates a long sequence of rules called value initialization [8.5-7]
The net effect of initializing a value is that an implicitly declared default constructor is never called . Instead, initialization initialization is called using a recursive element, which ultimately initializes to zero any primitive elements and calls the default constructor for any members that have a constructor declared by the user [8.5-5]
Value initialization applies even to primitive types — they will be initialized to zeros. [8.5-5]
a = int();
All this is really a moot point for most purposes. A class writer usually cannot assume that data members will be nullified during an implicit initialization sequence, so any self-checking class must define its own constructor if it contains primitive data elements that require initialization.
So, when does it matter?
There are times when common code wants to force initialization of unknown types. Initialization of initialization makes it possible to do this. Just remember that implicit null initialization does not occur if the user provides a constructor.
By default, the data contained in std :: vector is initialized with a value. This may prevent memory debuggers from identifying logical errors associated with other uninitialized memory buffers.
vector::resize( size_type sz, T c=T() ); // default c is "value-initialized"
Entire arrays of primitive types or simple-old (POD) structures can be initialized to zero using value initialization syntax.
new int[100]();
This post contains more detailed information about the options between versions of the standard, and also notes the case when the standard is applied differently in the main compilers.
nobar Feb 13 '11 at 5:59 2011-02-13 05:59
source share