tags:

views:

83

answers:

5

Can someone give me a real time need for enum data structure. As in example in some real system where it can be used? And what is the reason for having such a data structure. The example given was

enum colors_t {black, blue, green, cyan, red, purple, yellow, white}; 

But i felt, this is similar to string array. I am trying to understand the notion with which this feature was added to C++. Thanks !

  • Regards Sethu
+3  A: 

For example, consider this enumeration:

enum ip_packet_type {
    ip = 0,
    icmp = 1,
    igmp = 2,
    tcp = 6,
    udp = 17,
    // many others
};

This represents the IP protocol number in IP packets. Inside a packet, the protocol number is identified by a number rather than a name (8 bits at byte offset 9). This enumeration lets the program source refer to names like icmp and tcp rather than numbers. The number must be used inside the IP packet itself.

Greg Hewgill
+2  A: 

When you use an enum the values (such as black, green, etc.) are symbolic names that are represented internally as integers.

If you used a string array, then every time you wanted to use one of those values you’d need to copy the string, do a string compare, etc.

In addition, the enum can only contain the pre-defined values, which is desirable most of the time. If you represented the same thing as string there’s no such guarantee.

Nate
+2  A: 

An enumerator (like black in your example) is represented by an integer; comparing integers is fast. Comparing strings is not fast.

In addition, you get type safety. If you used strings like "black" and "blue", there's no guarantee that someone doesn't pass you "hot dog" when you expect a color. If you take a color_t, you are guaranteed to get a valid color (unless someone has gone and done something wrong).

James McNellis
+1  A: 

1) Enum vs Integers: Enums are easier to read and hence code is more maintenable.

2) Enum vs Strings: Enums are much more efficient (alsmo as effecient as integers).

Added advantage: Enums are automatically restricted to the range of values you predefine. This also means that they are useful only when the range of values is small.

JP19
+1  A: 

For the record, Bjarne Stroustrup talks about bringing enums into C++ in the book The Design and Exolution of C++ with the statement "C enumerations constitute a curiously half-baked concept. Enumerations were not part of the original conception of C and were apparently reluctantly introduced into the language as a concession to people who insisted on getting a form of symbolic constants more substantial than Cpp's parameterless macros." (section 11.7, "Cpp" refers to the C preprocessor, the rest of the section chronicles the decision to make each enum a separate type in C++ instead of all of them being ints as they originally were in C and early versions of C++).

enums are largely meant as a way of replacing #defines in earlier versions of C.

// based on UNIX file permissions
#define EXECUTE 1
#define WRITE 2
#define READ 4

vs.

const int EXECUTE = 1;
const int WRITE = 2;
const int READ = 4;

vs.

enum File_perms {
    EXECUTE = 1;
    WRITE = 2;
    READ = 4;
};

Which to use is largely a matter of personal taste. An enum does provide a form of documentation about what kind of values a variable should hold:

int permissions = 4; // Was that file permissions, database permissions, or something else?
File_perms perms = 4;

This is especially helpful in function signatures:

int fiddle_bits(int, int); // I can never remember if I pass the file permissions as the first or second parameter ...
File_perms fiddle_bits2(File_perms, int);

enums are allowed in switch statement case labels (as are #defines and const ints):

switch (perm) {
    case READ:
        ...
    break;
    ...
}

However, note that it is possible to assign numbers to enums that don't have a labeled value (C++0x adds an enum class that doesn't allow this):

File_perms perm = 7; // a.k.a., File_perms perm = EXECUTE | READ | WRITE;

If you ever see an enum with explicit values that are powers of 2, you can almost guarantee that it will be used this way.

Max Lybbert
enums were "not added to C++", they are a (quite dangerous) legacy from C. Plain 'C' enums are of limited use in C++, therefore the latest C++ specification draft (finally!) added type safe 'enum struct' and 'enum class' constructs.
paul_71
@paul_71: the Wikipedia article on enums agree with you, "C++ has enumeration types that are directly inherited from C's", but I'm not sure. I just checked my 1978 copy of "The C Programming Language" and at that time C didn't have enums, but in C89 they were there. And C++ started circa 1980. Funny "anacronism" disucssed in 1978 in TCPL: original C allowed declarations w/init like "int x 666;" but this was changed to "int x = 666;" because "int x (42+1);" resembled a function decl "enough to confuse compilers"; and it's STILL the "most vexing parse" in C++... :-)
Alf P. Steinbach
Hm, I don' have a c-standard at hand, so I trust you fully if you already looked it up. It's also true that C++ started around 1980, but the first C++ standard specification stems from 1998 where 100% compatibility with C (of that time) was guaranteed. In fact, it still is, since those unutterably unsafe 'enums' are not going to disappear...as are not the reasons for the 'vexing parses'...
paul_71
You guys are right. I had based my comments on an incorrect memory from "The Design and Evolution of C++." But that book says "I had no need for enumerations in the styles of programming I wished to support and no particular wish to meddle in the affairs of enumerations, so C++ adopted C's rule unchanged." I've edited the answer.
Max Lybbert