| Hazique35 (219) | |
|
I know what enumerations are but I don't really understand how to use them or what I could do with them. Could anyone give me an explanation of what they are, then show me an example of how they could be used? Thanks. | |
|
|
|
| Framework (3242) | |||||||
Wherever you use constants, you can use enumerations. In addition, enumerations can be use to tag switch cases with a name for easier readability. For example:
As you can see, the labels are much more descriptive and to the point than magic constants. Another use of enumerations is providing the ability to create a ranged-value variable. An instance of "Switch_Labels", for example, can only hold the enumerators that pertain to the "Switch_Labels" enumeration. This can be useful for tagging certain indices of an array. For instance:
By using enumerators, we can clearly see which wheel we're accessing. If we used magic constants, we would have to guess which wheel we're referring to. Other uses of enumerations are replacing pre-processor constants and bit-masks. For example:
There are probably more uses, but these are the most common. Wazzak | |||||||
|
Last edited on
|
|||||||
| Hazique35 (219) | |
|
Thank you | |
|
|
|
| Peter87 (3917) | |
| The last example will not work unless you have overloaded operator|. | |
|
|
|
| Framework (3242) | |
|
@Peter87: I omitted the overload for simplicity. I assumed Hazique35 had the common sense to overload it. Wazzak | |
|
Last edited on
|
|