Reputation: 1721
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 !
Upvotes: 1
Views: 4018
Reputation: 20021
For the record, Bjarne Stroustrup talks about bringing enum
s 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 int
s as they originally were in C and early versions of C++).
enum
s are largely meant as a way of replacing #define
s 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);
enum
s are allowed in switch
statement case
labels (as are #define
s and const int
s):
switch (perm) {
case READ:
...
break;
...
}
However, note that it is possible to assign numbers to enum
s 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.
Upvotes: 1
Reputation:
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.
Upvotes: 1
Reputation: 354969
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).
Upvotes: 2
Reputation: 19030
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.
Upvotes: 2
Reputation: 992707
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.
Upvotes: 3