Anonymous structs



  • I need to do that:

    union tColor
    {
      struct //anonymous struct
      {
        unsigned char r,g,b;
      };
    
      unsigned char channels[3];
    };
    

    This works as intended in MSVC:

    tColor color;
    color.r=100;
    color.g=150;
    color.b=200;
    color.channels[1]==150; //true
    

    But i still worry if this according to the C++ standard.

    Also I wonder if I can use anonymous arrays:

    union tColor
    {
      struct
      {
        unsigned char r,g,b;
      };
    
      unsigned char[3]; //anonymous array ?
    };
    

    to do the following:

    tColor color;
    color.r=100;
    color[1]=150;
    color[2]=200;
    

    Or I should define operator[] ?


Anmelden zum Antworten