schwarze@isaak.isa.de (Jochen Schwarze) (11/09/89)
Consider the following: struct color { enum { red, green, blue } type; }; main() { struct color favourite; favourite.type = red; } If this piece of code is compiled with g++ (1.35.0) or AT&T cfront (1.20) both compilers complain about `red' in line 10 being undefined or undeclared, resp. gcc (1.35.0) and Sun cc compile without problems. The C++ book by Stroustrup says nothing about an enumerators scope being restricted. Is this a compiler bug or a difference between c and c++? Thanks in advance. Jochen Schwarze Domain: schwarze@isaak.isa.de ISA GmbH, Stuttgart, West Germany UUCP: schwarze@isaak.uucp Bang: ...!uunet!unido!isaak!schwarze
thant@horus.sgi.com (Thant Tessman) (11/14/89)
In article <2113@isaak.isa.de>, schwarze@isaak.isa.de (Jochen Schwarze) writes: > > Consider the following: > > struct color { > enum { red, green, blue } type; > }; > > main() > { > struct color favourite; > > favourite.type = red; > } > > If this piece of code is compiled with g++ (1.35.0) or AT&T cfront (1.20) > both compilers complain about `red' in line 10 being undefined or > undeclared, resp. > > gcc (1.35.0) and Sun cc compile without problems. > In _Programming in C++_ by Dewhurst and Stark, on page 60 it mentions that 'enum' can be nested within a class. Although it doesn't outright say it, I would think that this implies that 'enum' scopes like anything else. I have used 'enum' this way (i.e. assuming it scopes) and I find it very usefull for making code more readable (and more typesafe, because C++ (2.0) actually typechecks enums as separate types). I would think gcc and Sun cc are busted. thant There are 336 dimples on the standard golf ball.
hansen@pegasus.ATT.COM (Tony L. Hansen) (11/16/89)
<> Consider the following: <> struct color { <> enum { red, green, blue } type; <> }; <> main() { <> struct color favourite; <> favourite.type = red; <> } <> If this piece of code is compiled with g++ (1.35.0) or AT&T cfront (1.20) <> both compilers complain about `red' in line 10 being undefined or <> undeclared, resp. gcc (1.35.0) and Sun cc compile without problems. < I have used 'enum' this way (i.e. assuming it scopes) and I find it very < useful for making code more readable (and more typesafe, because C++ (2.0) < actually typechecks enums as separate types). I would think gcc and Sun cc < are busted. This is one of the few places where C++ is not a superset of C. In C++, the scope of the enum is restricted to the class; in C, the enum has the same scope as the structure. Gcc and Sun cc aren't broken; they're doing exactly what they're supposed to do. Tony Hansen att!pegasus!hansen, attmail!tony hansen@pegasus.att.com