|
![](/i/fill.gif) |
On 10/22/2010 12:16 PM, Warp wrote:
> Darren New<dne### [at] san rr com> wrote:
>> Warp wrote:
>>> 'union' in C is basically a construct which exists to save some bytes
>>> of memory in some rare circumstances.
>
>> I have also seen it used for typecasting, but nowadays I think casting the
>> address of the thing to the "wrong" pointer type and then indirecting it
>> gets used more. I.e.; { long x = ...; float f = *(float*)&x; }
>
> The most clever use of 'union' I have seen was like this:
>
> union Matrix4x4
> {
> double m[4][4];
> struct
> {
> double m11, m12, m13, m14,
> m21, m22, m23, m24,
> m31, m32, m33, m34,
> m41, m42, m43, m44;
> };
> };
>
> This means you can access the matrix like "transform.m[1][2] = 5;"
> or like "transform.m23 = 5;";
>
> Of course in this case this is just a trick to have two naming
> conventions to access the same data (rather than to save memory).
As they call it, Syntactic sugar.
I've seen unions as a means of assisting with reading binary file data,
but generally stored in a struct.
the Variant data type used by COM is a union. again, it has a field that
tells what the variant is, but it basically is there to allow data of
any type to be passed around. Not exactly type safe, but necessary for
programs written in VB that insist on not declaring any variable as a
particular type. Or for when you don't know what data type you will be
handing off (e.g. the clipboard)
--
~Mike
Post a reply to this message
|
![](/i/fill.gif) |