Page 1 of 1

Overlapping class members

PostPosted: Aug 26, 2003 @ 6:36pm
by jont
Here's the deal - I have two one 16bit member variable, and two 8bit member variables within a C++ class.

Rather than each one occupy it's own position in memory, I'd like all three to share the same 16bit space as follows:

- The 16bit variable should reside at memory location X.
- The first 8bit variable should reside at memory location X.
- The second 8bit variable should reside at memory location X+1.

Therefore:

- Changing the value of the 16bit variable will change the value of both 8bit variables
- Changing the value of the first 8bit variable will change the value of the low byte of the 16bit variable
- Changing the value of the second 8bit variable will change the value of the high byte of the 16bit variable

I know this is possible in some way or another using unions, but I'm after a way of doing things where I don't have to place the variables within a structure.

Any ideas appreciated, however wacky!

Cheers

Jon

PostPosted: Aug 26, 2003 @ 7:15pm
by Pejo Software - Per

PostPosted: Aug 26, 2003 @ 7:57pm
by Pam

PostPosted: Aug 26, 2003 @ 8:17pm
by refractor

PostPosted: Aug 27, 2003 @ 10:20am
by jont
Thanks for the replies folks.

I'm already aware of the bit shifting and pointer solutions - I'm currently using the pointer solution but I'm concerned about the overhead of fetching the pointer variable every time I want to retrieve the value.

Ref: I'm interested to see what sort of assembly would be generated by the anon class types solution. Like the pointer solution, I'm concerned that the compiler may produce additional code to what it needs to generate.

PostPosted: Aug 27, 2003 @ 11:40am
by Orpe

PostPosted: Aug 27, 2003 @ 12:42pm
by Ref