Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!mit-eddie!uw-beaver!blake!ogccse!wm From: wm@ogccse.ogc.edu (Wm Leler) Newsgroups: comp.lang.c++ Subject: typedefs in C++ Message-ID: <1887@ogccse.ogc.edu> Date: 28 Nov 88 07:07:11 GMT Organization: Oregon Graduate Center, Beaverton, OR Lines: 58 In section 3.2 of the Wiener & Pinson C++ book, the following example is given to explain C++ declarations: typedef enum {red, green, amber} traffic_light_color; typedef struct intersection { traffic_light_color traffic_light; int number_cars_queued; int cumulative_number_cars; }; Now, I know this example is poor (and to prevent everyone on the net from posting what's wrong with it, I'll explain below), but the purpose of this posting is not to attack this book. What I want to know is, why does it compile? I used these declarations with the program void main() { intersection i; i.traffic_light = red; } and it compiled! And I added the word "struct" before the declaration of "intersection i" and the C compiler took it as well (both Vax and Sequent C compilers!). Can anyone give me a clue as to why this works? Now, the explanation I promised. In both C++ and C a typedef takes the form typedef ; as in typedef unsigned char BYTE; BYTE b; or in typedef struct { traffic_light_color traffic_light; int number_cars_queued; int cumulative_number_cars; } intersection; intersection i; In C++, struct names (and enum names) are automatically typenames, so typedefs are not necessary for struct definitions, and the last example can be simply written as struct intersection { traffic_light_color traffic_light; int number_cars_queued; int cumulative_number_cars; }; intersection i; Because of this, the example from the book doesn't need to be a typedef, and in fact, their example seems to be midway between C++ and C, but not really either. So, again, why does it compile? Wm Leler wm@cse.ogc.edu