Hello Enoizat!
Thanks for your reply! I do see that
enum class doesn't seem to be the best choice for looping through an array of structures. Yet I do feel, but have no confirmation for it actually being true, that, with an enum class, in a for loop, the array can never go out of bounds when stepping through it. Meaning that the first element and the last element in the enumerator class sets the limit. Of course that probably only is the case when both array and enum class contain the same amount of elements. (And, as you mentioned, the enumerator members are contiguous.) Or that, if they differ, only the number of elements can and will be accessible, specified in the enum class. (I hope I make sense).
Ex.: array[12], enum numbers { ONE, TWO, THREE, FOUR, FIVE };
In a loop, to my understanding, only items 0 to 4 would be displayed, whatever they might be, correct?
In my example, and in the program it was supposed to be used in, the enumerators were contiguous. (Months in both cases). The only difference being that in this example I chose to use a simple array, while my program uses array of structures.
-
I do agree as far as typecasting goes. With enumerator data type I can get by without casting. Below code-snippet being an example:
1 2 3 4 5 6 7 8 9 10 11 12
|
for (int index = JANUARY; index <= DECEMBER; index++)
{
// Do stuff
}
My book on the other hand explains it like this:
[code]
for (months = JANUARY; months <= DECEMBER; months = static_cast<monthNames>(months + 1)
{
// Do stuff
}
|
As an aside: When hovering over January, in my IDE, I also noticed that it has a similar call of sorts as it looks like would it be an enumerator class: enumName::JANUARY
Ultimately, for the programming project it was meant to be used in, I decided to go with enumerator data type.
-
What I learned from your article, of which the code listings are a case in point of 'code = too advanced for my current level of understanding', is, that, even those purposes I had in mind for as ideal candidates for an enum class, aren't very well suited. For instance in a menu-driven program. The code becomes verbose very quickly, and looks rather clunky ...
Yet, going by what you said, and I allow myself to quote you:
My personal conclusion is: I can’t see any error in your code, but as far as I can see its main advantage consists in a neater syntax when randomly accessing an array, not when getting through it by a loop. |
Enumerator class would make it the perfect fit for my current project. (Once I find a way to implement what I have in mind.) It involves allowing a user to alter data in an array of structures after all data has been entered. Meaning, each member of an array of structures should be accessible and alterable. (Not sure how I would pull this off in practice, but, I will (have to) find a way to make it happen. :-))
Btw. If you, or any other member of this most helpful community, would happen to know of any other web-resources I can learn more about enum class, please go ahead and provide a link to it. (Resource-Language doesn't matter). The only important thing is that it is newbie friendly, or more precisely tailored to my current level, in case it contains code listings ... (I tried, but failed miserably, to find what I was looking for.)