Union with anonymous struct

Discussion in 'C++' started by JKop, Oct 26, 2004.

  1. JKop

    JKop Guest

    You know how from time to time, you want to have an array which you can
    access via:

    array_name[element];

    But also, you'd like the more user-friendly option:

    array_name.element1
    array_name.element2

    I believe that some people use a union with an anonymous struct for this
    (which is not legal Standard C++) as follows:

    union Week
    {
    unsigned days[7];

    struct
    {
    unsigned monday;
    unsigned tuesday;
    ... //and so on
    };
    } christmas_week;


    Well firstly, even if anonymous structs *were* legal, the above code would
    still not necessarily work as expected - to be specific, "tuesday" doesn't
    necessarily have the same address as days[1], and why? padding.

    So... the following is my way of doing it. The only drawback is that it's no
    longer an aggreagate nor a POD:

    struct Week
    {
    unsigned days[7];

    unsigned &monday;
    unsigned &tuesday;
    ... //and so on

    Week() : monday( days[0] ), tuesday( days[1] ) //and so on
    };


    -JKop
    JKop, Oct 26, 2004
    #1
    1. Advertising

  2. JKop

    Chris Theis Guest

    "JKop" <> wrote in message
    news:1xofd.40039$...
    >
    > You know how from time to time, you want to have an array which you can
    > access via:
    >
    > array_name[element];
    >
    > But also, you'd like the more user-friendly option:
    >
    > array_name.element1
    > array_name.element2
    >

    [SNIP]

    Whether the second option is more userfriendly is arguable. However, you
    could use a map with strings as keys if you deem this more legible or
    user-friendly.

    Chris
    Chris Theis, Oct 26, 2004
    #2
    1. Advertising

  3. JKop

    Arijit Guest

    Good work. However, your struct will probably require twice as much
    memory as the union solution.

    Also, padding shouldn't be a problem with ints, but there certainly is
    no definite way to tell.

    BTW, why is anonymous structs illegal in unions ? Will allowing it
    create any specific problem ?

    -Arijit

    JKop <> wrote in message news:<1xofd.40039$>...
    > You know how from time to time, you want to have an array which you can
    > access via:
    >
    > array_name[element];
    >
    > But also, you'd like the more user-friendly option:
    >
    > array_name.element1
    > array_name.element2
    >
    > I believe that some people use a union with an anonymous struct for this
    > (which is not legal Standard C++) as follows:
    >
    > union Week
    > {
    > unsigned days[7];
    >
    > struct
    > {
    > unsigned monday;
    > unsigned tuesday;
    > ... //and so on
    > };
    > } christmas_week;
    >
    >
    > Well firstly, even if anonymous structs *were* legal, the above code would
    > still not necessarily work as expected - to be specific, "tuesday" doesn't
    > necessarily have the same address as days[1], and why? padding.
    >
    > So... the following is my way of doing it. The only drawback is that it's no
    > longer an aggreagate nor a POD:
    >
    > struct Week
    > {
    > unsigned days[7];
    >
    > unsigned &monday;
    > unsigned &tuesday;
    > ... //and so on
    >
    > Week() : monday( days[0] ), tuesday( days[1] ) //and so on
    > };
    >
    >
    > -JKop
    Arijit, Oct 26, 2004
    #3
  4. "Arijit" <> wrote in message
    news:...
    > Good work. However, your struct will probably require twice as much
    > memory as the union solution.
    >
    > Also, padding shouldn't be a problem with ints, but there certainly is
    > no definite way to tell.
    >
    > BTW, why is anonymous structs illegal in unions ? Will allowing it
    > create any specific problem ?
    >


    Anonymous structs are not legal at all, in unions or otherwise.

    john
    John Harrison, Oct 26, 2004
    #4
  5. JKop

    Ron Natalie Guest

    John Harrison wrote:

    >
    > Anonymous structs are not legal at all, in unions or otherwise.
    >

    There's not even a definition of that term. But if you take the
    C++ defintion of anonymous union and replace union with struct,
    it isn't legal.
    Ron Natalie, Oct 26, 2004
    #5
  6. JKop

    Derek Guest

    JKop wrote:
    > So... the following is my way of doing it. The only drawback
    > is that it's no longer an aggreagate nor a POD:


    Nice, but the other drawback is that it's twice the size of
    the original structure.

    > struct Week
    > {
    > unsigned days[7];
    >
    > unsigned &monday;
    > unsigned &tuesday;
    > ... //and so on
    >
    > Week() : monday( days[0] ), tuesday( days[1] ) //and so on
    > };
    Derek, Oct 26, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    664
    Matt Garman
    Apr 25, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    869
    Chris Torek
    Apr 26, 2004
  4. Michael B Allen

    Anonymous Union Member Access

    Michael B Allen, Sep 3, 2004, in forum: C Programming
    Replies:
    1
    Views:
    457
    xarax
    Sep 3, 2004
  5. David Resnick
    Replies:
    7
    Views:
    480
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F
    Oct 29, 2007
Loading...

Share This Page