Initializers
I'm very confused among different types of initializations:
zero-initialize:
scalar: set to 0
class/array: initialize recursively
union: initialize first member
reference: no-op
default-initialize:
class: call default constructor
array: initialize recursively
reference: ill-formed
others: no-op
value-initialize:
class with user-provided constructor: call default constructor
non-union class without user-provided constructor: zero-
initialise and call non-trivial constructor
array: initialize recursively
reference: ill-formed
others: zero-initialize
implicitly-defined default constructor:
default-initialize base classes and members recursively
When the direct initializer is (), the object is value-initialized but
consider the following:
#include <iostream>
struct A
{
A()
{
std::cout << "test" << std::endl;
}
int x;
};
struct B
{
// non-trivial implicit default constructor
A a;
int x;
};
A x; // x is zero-initialized and then default initialized so that x.x
== 0
int main()
{
A *p = new A; // p->x is not determined.
A *q = new A(); // q->x is also not determined?! A
standard caveat?!
delete p;
delete q;
B *r = new B; // default-initialized recursively: s->x
and s->a.x are both indeterminate.
B *s = new B(); // zero-initialized first: s->x and s->a.x
are both 0
}
Also, how does zero-initializing a reference make sense?