Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2007

Tip: Looking for answers? Try searching our database.

"union" syntax

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 19 Sep 2007 19:52 GMT
Hi all,

I'm struggling to do something really basic.  How do I declare a structure
that has a union that maps an array of 3 chars with 3 distinct char
variables.  In other words, I want the following two statements to put the
same value into the same byte in the structure:

 myStruct.Vars[1] = 5;
 myStruct.Var1 = 5;

TIA - Bob
Peter Oliphant - 19 Sep 2007 19:58 GMT
How about just including this somewhere:

#define myStruct.Var1   myStruct.Var[1]

To be honest, not sure if the above works, and it is a purely syntactical
solution, and may not address the reasons WHY you want this capability.

[==Peter==]

> Hi all,
>
[quoted text clipped - 7 lines]
>
> TIA - Bob
Bob Altman - 19 Sep 2007 20:44 GMT
Well, that's certainly thinking outside of the box.  But I'd be surprised if
it compiles.

As for why I want this:  I have a structure that is used to pass data into
my public API:

struct MyStates {
 BOOL X_Axis_Enabled;
 BOOL Y_Axis_Enabled;
 BOOL Z_Axis_Enabled;
}

Now, I've decided that it would be very convenient for me to be able to
access the members of this structure as though they were members of an
array, like this:

struct MyStates {
 BOOL Enabled[3];
}

But I can't change the structure in a way that will break existing code that
accesses the individual member variables.

 - Bob

> How about just including this somewhere:
>
[quoted text clipped - 4 lines]
>
> [==Peter==]
David Wilkinson - 20 Sep 2007 04:29 GMT
> Well, that's certainly thinking outside of the box.  But I'd be surprised if
> it compiles.
[quoted text clipped - 18 lines]
> But I can't change the structure in a way that will break existing code that
> accesses the individual member variables.

Bob:

How about

struct MyStates
{
  BOOL X_Axis_Enabled;
  BOOL Y_Axis_Enabled;
  BOOL Z_Axis_Enabled;
  BOOL& operator[](int i)
  {
    BOOL* enabled[3];
    enabled[0] = &X_Axis_Enabled;
    enabled[1] = &Y_Axis_Enabled;
    enabled[2] = &Z_Axis_Enabled;
    return *(enabled[i]);
  }
};

int main()
{
  MyStates myStates;
  myStates[0] = TRUE;
  myStates[1] = FALSE;
  myStates[2] = TRUE;
  return 0;
}

Signature

David Wilkinson
Visual C++ MVP

Doug Harrison [MVP] - 19 Sep 2007 20:46 GMT
>How about just including this somewhere:
>
>#define myStruct.Var1   myStruct.Var[1]
>
>To be honest, not sure if the above works

It does not. You can't define a macro name that contains a dot.

Signature

Doug Harrison
Visual C++ MVP

Doug Harrison [MVP] - 19 Sep 2007 20:46 GMT
>Hi all,
>
[quoted text clipped - 5 lines]
>  myStruct.Vars[1] = 5;
>  myStruct.Var1 = 5;

You could do it with:

union U
{
  struct
  {
     char Var0;
     char Var1;
     char Var2;
  };
  char Vars[3];
};

This makes use of an anonymous struct, which is a VC extension, so it's not
going to be portable.

Signature

Doug Harrison
Visual C++ MVP

Bob Altman - 19 Sep 2007 21:30 GMT
Thanks Doug, that's just what I was looking for.

 - Bob

>>Hi all,
>>
[quoted text clipped - 22 lines]
> not
> going to be portable.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.