Re: Question about "enums"
- From: John Devereux <jdREMOVE@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 03 May 2006 09:16:51 +0100
Ian Collins <ian-news@xxxxxxxxxxx> writes:
John Devereux wrote:
Ian Collins <ian-news@xxxxxxxxxxx> writes:
Nothing wrong with that, I was referring to code that assigned invalid
values to an enum.
But how is the the compiler supposed to *know* they were invalid? InSorry if I got carried away with the snippers...
the code you snipped, I only mentioned the "special" values in the
enum, yet in fact any integer could be "valid".
The compiler would know because it knows the legal set of values for the
enumeration. For this to work, math operations on enums will have to be
removed. In my opinion, they don't make sense, given a set of integers
{ 1, 5, 7, 9 } what does incrementing a member of the set do?
Restoring your code:
enum
{
START=000,
REJECT,
QUEUE,
BRAKE,
/*...*/
ALARM=020,
TEST,
NSIGNALS=040
};
turn_on(REJECT);
turn_off(TEST);
/* hardware test */
for(i=0; i<NSIGNALS; i++)
{
turn_on(i);
delay();
turn_off(i);
}
I had assumed that i was an int (you didn't show the declaration).
Sorry, yes, i was an int. But turn_off() could be declared a taking an
enum argument. But actually I suppose it could just as well be
declared taking an int, and in fact that is what I do in practice.
I thought your suggestion was that compilers should reject assignments
of values that are not in the enum declaration.
Indeed it is.
So it would be OK to convert an enum to an arbitrary int, but not an
int to an enum type?
--
John Devereux
.
- Follow-Ups:
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- References:
- Re: Question about "enums"
- From: Thad Smith
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- From: Thad Smith
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- From: Keith Thompson
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- From: Keith Thompson
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- From: John Devereux
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- From: John Devereux
- Re: Question about "enums"
- From: Ian Collins
- Re: Question about "enums"
- Prev by Date: Re: help me learn C
- Next by Date: How do I print this?
- Previous by thread: Re: Question about "enums"
- Next by thread: Re: Question about "enums"
- Index(es):
Relevant Pages
|