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.


When a user-defined enumeration is instantiated what type is created by the NET Framework to represent it?

When a user-defined enumeration is instantiated in the .NET Framework, it is represented as a distinct type derived from the System.Enum class. This new enumeration type is based on an underlying integral type, typically int by default, but it can also be specified as byte, sbyte, short, ushort, long, or ulong. The enumeration values are essentially named constants of this underlying integral type, allowing for type-safe representation of a set of named values.


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.

Related Questions

Different types of enumeration?

Enumeration is a systematic listing or counting of items, which can be categorized into several types. Exhaustive enumeration involves listing all possible items within a defined set, while partial enumeration includes only a subset of items. Ordered enumeration arranges items in a specific sequence, whereas unordered enumeration presents items without any particular order. Additionally, combinatorial enumeration focuses on counting combinations or arrangements of items, often used in mathematics and statistics.


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.