Assuming you haven't modified a
before calling test()
, a
has a value of zero, because objects with static storage duration are zero-initialized when the program starts.
d[0]
has a value of zero, because the constructor invoked by std::vector<int> d(1)
has a second parameter that takes a default argument; that second argument is copied into all of the elements of the vector being constructed. The default argument is T()
, so your code is equivalent to:
std::vector<int> d(1, int());
You are correct that b
has an indeterminate value.
f.a
and *c
both have indeterminate values as well. To value initialize them (which for POD types is the same as zero initialization), you can use:
Foo f = Foo(); // You could also use Foo f((Foo()))
int* c = new int(); // Note the parentheses
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…