🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Jochen Schwarze Wed, 08 Nov 1989 21:06
scope of enumerators
#560
Author: Jochen Schwarze
Date: Wed, 08 Nov 1989 21:06
30 lines
1285 bytes
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
Re: scope of enumerators
#561
Author: Thant Tessman
Date: Mon, 13 Nov 1989 17:16
36 lines
1551 bytes
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.
Re: scope of enumerators
#562
Author: Tony L. Hansen
Date: Thu, 16 Nov 1989 15:28
27 lines
1655 bytes
<> 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads