'In struct nested union/array member default initialization compiles, but is not happening correctly?
I am trying to initialize array members at struct declaration in the following struct with nested union & array:
struct Nested
{
union
{
short sArray[5] = {42};
float fVal; // Must NOT be initialized - obviously, only 1 member of a union can be
};
double dArray[5] = {77.7};
};
While the code compiles just fine, only first elements of both arrays are initialized, when running/debugging the code..
sArray[0] is set to 42, remaining elements are all set to 0 dArray[0] is set to 77.69999, remaining are all set to 0
All other answers I found, mention initialization at instance declaration, but not default init in struct/class declaration. I have not seen/found whether this syntax is enabled (for array members too) by gnu c++17. However, since it compiles WO warning one would assume it should correct. Am I doing something wrong ?
Edit: Or how do I simply initialize my arrays ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
