when ctor A :: A () is called, how is the data element A :: i initialized
If no initializer is provided, the default initialization rules apply. Your constructor does not initialize A::i , so it has remained uninitialized; this value is indefinite. No doubt about that. Excerpt from the default initialization documentation :
If T is a class type, the constructors are considered and resolved to overload with respect to the empty argument list. The selected constructor (which is one of the default constructors) is invoked to provide an initial value for the new object.
why only a1.i and aa1 [0 ~ 2] .i are initialized equal to 0, while others are not initialized?
The global data memory is initialized to zero, i.e. the entire section is zeroed, so the global A::i initialized to 0 . Note that the constructor will not do this. Excerpt from the documentation :
Static initialization
[...]
2) For all other nonlocal static and stream local variables, Zero initialization takes place. In practice, variables that will be initialized with zeros are placed in the .bss segment of the program image, which does not take up disk space and resets the OS when the program loads.
However, for vector , vector itself is in non-local static memory, and its elements are allocated in free storage (heap), and therefore their members are also not initialized.
legends2k
source share