1. Technology

Using Enum Variables

What are they for?

By

This is the third tutorial in the series Online C++ Tutorial for beginners and is about enum variables. If you want to start at the beginning, begin with the first Online C++ Tutorial.

This is a rewriting of the original C++ tutorials into single page bite size tutorials which can usually be run on either codepad.org or ideone.com.

What are Enums?

Enums are a handy way of declaring a type that gives names to a range of int constants. Only those constants can be assigned to a variable of the type and assigning say an int value will give compile errors. This is one of the way that C++ enums are better than C.

That said, C++ enums are not fully typesafe as this example below shows. Typesafety means only values of the specified type can be assigned to an instance of it.

Behind the scenes an enum variable is stored as an int. You could avoid using enums by declaring the values as const ints (or worse #define) but (a) you'd clutter your code up and (b) you'd lose what type safety thee is.

Here's an example:

#include <iostream>

enum trafficlights {red,yellow,green};

int main(int argc, char* argv[])
{
    trafficlights light = red;
    
    std::cout << light << " " << trafficlights::green << std::endl;
    if (light ==2) { //Not exactly type safe!
        std::cout << "Light is green" << std::endl;
    }
    return 0;
}

The enum type trafficlights holds values that are coded as red, yellow and green. Note this line just declares the type trafficlights. In the main function a variable of that type is declared and assigned the green enumeration value.

When this is run, it outputs:

0 2

Run it online with this Ideone.com link.

The if statement runs but as this expression is false 0 == 2, it doesn't output "Light is green". However it's the fact that the compiler doesn't reject the if statement that means that the typing isn't as strong or safe as it should be.

Note I also qualified the colour green with a trafficlights:: qualifier, but that isn't needed. This will do as well.

std::cout << light << " " << green << std::endl;

Enums are Int Values

In case you haven't guessed it, the compiler allocates int values starting with 0 to each member of the enum and incrementing by one for each subsequent element. So red being first is 0, yellow is 1 and green is 2.

You can specify any value by adding = value after a enum member. So if we add ,orange=128,purple after green then orange takes the value 128 and purple is one up from that at 129 as this short example shows.

#include <iostream>

enum trafficlights {red,yellow,green,orange=128,purple};

int main(int argc, char* argv[])
{
    std::cout << orange << " " << purple << std::endl;
    return 0;
}

Run that and you get:

128, 129

Using Enums

You could do without them and use #define instead but defines are global to the whole file, whereas enums follow C++'s scope rules so by declaring it and using it completely within a function or even a class it can't be accessed from outside that function.

Unlike C which requires you to prefix every enum variable declaration with the word enum, C++ does not.

Anonymous Enums

You can declare a set of enum types like in this example without specifying the type name.

#include <iostream>

enum {alpha,beta, gamma };

int main(int argc, char* argv[])
{
    std::cout << alpha << " " << beta << std::endl;
    return 0;
}

When run it outputs 0 1, as you'd expect. You are basically declaring constants.

Converting enums to ints.

This is done without any casting needed, just a line like: int a = enum value;
Here's a longer example showing this in use:

#include <iostream>

enum {alpha,beta, gamma };

int main(int argc, char* argv[])
{
    int a = alpha;
    std::cout << a << " " << alpha << " " << beta << std::endl;
    return 0;
}

When run, that outputs 0 0 1 showing that the value of the enum is an int.

Declaring a type and variable

As with C the enum declaration can also create and optionally initialize a variable of the declared type. The line below declares an enum type radiation with members alpha, beta and gamma and declares an instance of this type fred which is initialized to gamma.

enum radiation {alpha,beta, gamma } fred=gamma;

In the next tutorial I'll look at arrays.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C++ Programming
  5. Learn C++ Programming
  6. Online C++ Programming Tutorial for Beginners - Lesson Three - Enum Variables

©2014 About.com. All rights reserved.