< previous page page_539 next page >

Page 539
Here is the precise rule:
Implicit type coercion is defined from an enumeration type to int but not from int to an enumeration type.
Applying this rule to the statements
someInt = DOG;    // Valid
inPatient = 2;    // Error
we see that the first statement stores 2 into someInt (because of implicit type coercion), but the second produces a compile-time error. The restriction against storing an int value into a variable of type Animals is to keep you from accidentally storing an out-of-range value:
inPatient = 65;    // No
We said earlier that an enum declaration is similar to a Typedef statement and several const declarations, all of type int. For example, the declaration
enum Boolean {FALSE, TRUE};
is like the sequence of statements
typedef int Boolean;
const int FALSE = 0;
const int TRUE = 1;
However, there is an important difference. Because of the type coercion rule, the compiler issues an error message at the assignment statement in the following code:
enum Boolean {FALSE, TRUE};

Boolean isGreater;
float   a;
float   b;
  .
  .
  .
isGreater = (a > b);   // Error
The data type of the right-hand side is int. (Remember that a relational expression yields the int value 1 or 0, meaning TRUE or FALSE.) Type coer-

 
< previous page page_539 next page >