I'm trying to initialize an array of objects with 0 like this (simplified code of a more complex project) :
#include <iostream>
struct Vector {
float s[4];
Vector() {}
static Vector zero() {
Vector v;
v.s[0] = 0;
v.s[1] = 0;
v.s[2] = 0;
v.s[3] = 0;
return v;
}
};
struct Test {
Vector v[4] = { Vector::zero() };
};
int main(int argc, char** argv)
{
Test t;
for (int i = 0; i < 4; i++) {
printf("%f %f %f %f\n", t.v[i].s[0], t.v[i].s[1], t.v[i].s[2], t.v[i].s[3]);
}
return 0;
}
This code should print all 0, but sometimes it prints different values. Looks like only the first element of the array is initialized. But if I write float x[4] = { 0 }, then all elements of the array x are initialized with 0. What is the difference and where in the C++ standard can I read about this behavior?