Nameless struct / union as a private member of a class

I am working on some code and stumble upon something like this:

class Foo
{
private:
  union {
    byte bar;
    struct {
      byte baz1;
      byte baz2;
    };
  };
};

Now I am compiling with a Level 4 warning in VS 2010 (pure unmanaged), and of course, VS complains that the unnamed structure / join is a non-standard extension (C4201 warning), and I want to fix this warning.

Is there any reason someone would prefer:

class Foo
{
private:
  byte bar;
  byte baz1;
  byte baz2;
};

Or will any reason why change the first to the last be violated?

+3
source share
3 answers

, - . ( ).

/ . URL-:

++ ?

2 1, 1 2 , ( ).

+1

4 .

, , - union struct. . union , , .

, :

class Foo
{
private:
  union {
    int bar;
    struct {
      int baz1;
      int baz2;
    } thing_;
  };
};

, Foo thing_ union.

+1

Of course, there is a reason. Your solution requires 50% more storage space as a merged version. Moreover, with unification foo.bar = foo.baz1for allFoo foo;

0
source

All Articles