Main Index : Reference :

enum


Description

Declares a global enumerated list of constants.

Usage

enum
{
  item,
  item,
  [...]
  item
}

enum
{
  item = [int] value,
  item = [int] value,
  [...]
  item = [int] value
}

Parameters

item is the name of the constant.
value is an optional value.

Declaration

enum provides an easy way to create a list of enumerated global constants. There's no enum type in C.O.F.F.E.E. so the constants can be used everywhere. The constants are automatically numbered sequentially, but one can also specify the values of the constants manually. All enums must be declared globally.

The advantage of using an enum instead of just arbitrary chosen integers for things like object types is that it gives the code a much clearer and more readable look. An enum also automatically makes sure that all occurences of a constant use the same value.

Example

// An example of how using enums instead of numbers
// can make the code clearer and easier to read.

enum
{
  CL_BLUE,
  CL_RED
  CL_GREEN
}

set_color(fld, color)
{
  switch(color)
  {
    case CL_BLUE:
      fld->SetColor(0,0,1);
      break;
 
    case CL_RED:
      fld->SetColor(1,0,0);
      break;
   
    case CL_GREEN:
      fld->SetColor(0,1,0);
      break;
  }
}

function()
{
  var fld = GetFld();
  set_color(fld, CL_BLUE);
}


// Assigning the constant values manually:

enum
{
  AL_LEFT = 1000,
  AL_CENTER = 2000,
  AL_RIGHT = 3000
}

See Also

new(), const, var