Problems with constant members of a union within a structure

I wanted to declare some parts of the structure as const, because I pass it to a third party in an API call. I plan to fill in the values, discarding const, using a C-style cast. This allows me to set them, but the API user knows that these are input values, since they cannot easily set them.

Going to a simple example, I have

struct mystruct
{
  union valuesUnion
  {
    const int32_t integer;
    const double real;
    valuesUnion() : integer(0), real(0.0) {}
  } values;
  mystruct() : values(valuesUnion())
  {
  }
};

int main()
{
  mystruct a;
}

Please note that I had to add a constructor for "mystruct" or you received a compilation error due to uninitialized constant variables.

But I get an error -

g ++ Z.cpp -o Z Z.cpp: In the constructor âmystruct :: valuesUnion :: valuesUnion () â: Z.cpp: 11: 5: error: initialization for several members âmystruct :: valuesUnionâ make: * [Z] Error 1

, -

valuesUnion() : integer(0) {}

g++, Visual Studio 2012 -

valuesUnion:: real ': /

?

.

+3
1

, . , .

union valuesUnion

struct valuesUnion

, .

+2

All Articles