answersLogoWhite

0

We use enumerations to identify a group of constants in a more meaningful way than would be possible with an integral type. Consider the following:

int toggle (int traffic_light)

{

if (traffic_light<0 traffic_light>3) return -1; // invalid argument!

traffic_light=traffic_light==3?0:traffic_light+1;

return traffic_light;

}

The int argument, traffic_light, gives us some indication as to its purpose, but the body of the function is difficult to read. It's difficult to digest the logic and the literal constant 3 looks suspiciously like a "magic number". Worse, the function returns errors through the return value.

Let's re-write the function with an enumerated type:

enum traffic_light {green, amber, red, red_amber};

traffic_light toggle (traffic_light current)

{

switch (current)

{

case green: current=amber; break;

case amber: current=red; break;

case red: current=red_amber; break;

default: current=green;

}

return current;

}

While the body of the function is more verbose, the logic is much clearer. We can see from the enumeration that a traffic_light object only has four possible states and that the function cycles from one state to the next, just as a real traffic light should.

Upon closer examination, we note that a traffic_light behaves exactly as if it we're a 2-bit unsigned integer and that all we're doing is incrementing the integer, cycling back to 0 when both bits are set. So we could effectively implement the toggle function with an operator overload:

traffic_light& operator++(traffic_light& lhs)

{

return lhs = (lhs==red_amber)?green:(traffic_light)(((int) lhs)+1);

}

Operator overloads are often difficult to read, however all operators must be intuitive; they should all work exactly as we expect them to work. The above actually replicates the middle line of the original function, casting to int to perform the increment.

With this in place, our toggle function becomes:

traffic_light toggle (traffic_light current)

{

return ++current;

}

User Avatar

Wiki User

8y ago

What else can I help you with?

Continue Learning about Engineering

Explain how implicit enumeration is used to solve integer programming problems.?

Implicit enumeration (or "additive algorithm") is used to solve 0/1 LP problems


What is method of enumeration called?

The method of enumeration is commonly referred to as &quot;enumeration&quot; itself, which involves systematically listing all possible outcomes or elements of a set. In statistics and research, it may also be referred to as &quot;counting&quot; or &quot;cataloging.&quot; This technique is often used in probability, combinatorics, and data analysis to ensure comprehensive coverage of all possibilities.


What is meant by enum in C programming?

The enum keyword means enumeration.


Disadvantages of enumeration in c?

The big disadvantage I've found is that you might mistake it for a user-defined type. It's not; you still have to use int for your variable type and compilers will not do anything to help restricted assignment to values from the enumeration.


What is the difference between enumeration and set of preprocessor define in c?

An enumeration is a group of named integral constants. An enumeration is also a type thus it provides type safety. When passing a constant to a function, an enumeration eliminate values that are not part of the group, thus allowing the compiler to catch errors that would otherwise slip through. A set or pre-processor definitions (or macro definitions) do not provide any type safety and are never actually seen by compiler (they are processed before the compiler sees the resultant code). Macros are nothing more than a text-replacement system that effectively allow programmers to create a language within the programming language and thus create code that would be difficult if not impossible to produce using the language alone. However, when the language provides a simpler, type-safe mechanism, it is always better to utilise it and thus enlist the help of the compiler to eliminate errors. Macros do have their uses, particularly when debugging, but they must be used only when it is appropriate to do so. Enumerations are preferred over a set of macro definitions every time. Some languages also permit enumeration classes, which gives stronger guarantees than a "plain" enumeration, not least eliminating the implicit promotion between enumerated types and integral types.

Related Questions

What are various data types?

Integer types, floating point types, Boolean types, character types, string types, pointer and array types, reference types, enumeration and other user-defined types.


What is an enumeration sentence?

enumeration sentence can be used in a sentence as such. enumeration means numbered words..


What does Amendment Nine talks about the enumeration of rights. Enumeration means a .?

Enumeration means a LIST.Hope this answered your question.


What is the Tagalog word for enumeration?

Tagalog word of enumeration: isa-isahin


Why do you have enumeration?

There are different methods for naming a integer constant, one such method is enumeration. Enumeration is created by using the keyword enum.


What does a Sample of a letter with enumeration look like?

sample of letter with enumeration


Enumeration of steps of scientific method?

hi i have ask which of the following is not a method enumeration


What is total enumeration in research?

there are two types of data collection: 1. complete/total sampling- all members of the population are measured 2. partial sampling- a proportion of members of the whole population is measured. total enumeration is preferred for certain types of data. it has a high level of accuracy and provides a complete statistical coverage over space and time.


What is the Tagalog of the word enumeration?

that means step by step


What is enumeration system?

An enumeration system is a way of categorizing and listing items based on specific criteria or characteristics. It is used to organize and classify information for easy reference and analysis. In computer science, enumeration systems can also refer to data types that define a set of named constants.


What rhymes with altercation?

Enumeration.


What is a population count known as?

Enumeration is one possible answer. The census is another.