Assignment To Enum Types

From RAD Studio
Jump to: navigation, search

Go Up to Enumerations Index

The rules for expressions involving enum types have been made stricter. The compiler enforces these rules with error messages if the compiler switch -A is turned on (which means strict ANSI C++).

Assigning an integer to a variable of enum type results in an error:

enum color
{
  red, green, blue
};
int f()
{
  color c;
  c = 0;
  return c;
}

The same applies when passing an integer as a parameter to a function. Notice that the result type of the expression flag1|flag2 is int:

enum e
{
  flag1 = 0x01,
  flag2 = 0x02
};
void p(e);
void f()
{
  p(flag1|flag2);
}

To make the example compile, the expression flag1|flag2 must be cast to the enum type: (e)(flag1|flag2).