Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Union with anonymous struct

Reply
Thread Tools

Union with anonymous struct

 
 
JKop
Guest
Posts: n/a
 
      10-26-2004

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
 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      10-26-2004

"JKop" <(E-Mail Removed)> wrote in message
news:1xofd.40039$(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
 
Arijit
Guest
Posts: n/a
 
      10-26-2004
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 <(E-Mail Removed)> wrote in message news:<1xofd.40039$(E-Mail Removed)>...
> 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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-26-2004

"Arijit" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-26-2004
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.


 
Reply With Quote
 
Derek
Guest
Posts: n/a
 
      10-26-2004
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
> };



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Getting sizeof an anonymous struct declared inside a union David Resnick C Programming 7 10-29-2007 05:54 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments