Things I still dont really get

Started by
3 comments, last by spoulson 18 years, 2 months ago
Well ive been at this for 1-2 years and when I look at some of the code from widely used headers (windows.h) (string,vector ect) there are a few things that pop out at me as "that really weird syntax" here it is

union
{
   X
   Y
   Z
}

ok so WHAT IS a union and what advantage does it have over things like structs
and classes I didn't find any good articles on this site about unions and no good explenation in any of the books I have.

another is an implementation of typedefs that ive seen

typedef struct NAME{
    X
    Y
    Z
} NAME, *NAME;

what does this mean? in the perticular refrence i thinking of the second Name 
at the bottom is actually a pointer but im not sure if thats relevent

how is this diffrent from 
struct Name
{
  X
  Y
  Z
}
I guess the sorce of most of my confusion is from the weird combinations of the key works 'typedef, struct and union' any clarification, and if this topic is just too broad feel free to point me to a link thanks [smile] very strange
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
1. A union stores multiple values (usually of different types) in the same memory address. (Actually, I don't know what the standard says about where the data is stored, but the basic idea is that you only use as much memory as is required for the 'largest' type in the union). Here is the first hit from a google for 'c++ unions'.

2. Aside from a couple of syntax errors, the first struct example you have there is C-style (I'm pretty sure) and typedef's both the struct itself and a pointer to the struct, while the second example is how you would declare a struct in C++.
Quote:
what advantage does it have over things like structs


One "advantage" is the ability to use more than one way to access contained data:
union my_vector{  struct  {    int x, y, z;  } point;  int vector[3];};...my_vector v;// Showing two ways to access the same variablev.point.x = 0;v.vector[0] = 0;...


When implementing the [] operators of a class, the vector is more handy, while accessing the components directly, the point representation is more handy.
Just a matter of taste I guess.
The first time I saw this way of keeping local data was in the source code of Descent 1.

I have also seen this type of construct used in the implementation of a simple script language. Im sure you have seen script languages where a variable can contain "anyhing". Integer, float, string. Or pointer to function (used for so called built-ins) and so on. My guess is they did so to preserve memory space.
However, I suspect implementing a script language that way is "old-style" these days.

I cant think of any other/better uses right now...
Re: the second issue.

In C, structures don't become first-class citizens of the type world. If you create a structure MyStruct, and want to instantiate it, you have to write "struct MyStruct MyVariable;" because without that "struct" keyword "MyStruct" doesn't mean anything to the compiler.

That's not terribly nice to read, so the typedef assigns a first-class name to the structure. If you change the structure's name to "_tagMYSTRUCT" but typedef it to "MyStruct," you can then do the C++-style thing of just writing "MyStruct MyVariable."

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:
typedef struct NAME{    X    Y    Z} NAME, *LPNAME;

This syntax is a type definition of 3 types: struct NAME, NAME, and LPNAME. 'struct NAME' and 'NAME' are identical. 'LPNAME' is defined as a pointer to 'NAME'.

You could expand the syntax like this:
struct NAME {   X   Y   Z};typedef struct NAME NAME;typedef NAME *LPNAME;

This topic is closed to new replies.

Advertisement