Is it possible to cycle and extract its value for every item in an enum, e.g:
enum {
item 1 = 1,
item 2 = 7,
item 3 = 'B'
} items;
To say, an array. Possible?
Do you mean to construct an array with one element for each value of the enum? Such that you would end up with an array {1, 7, 'B'}
?
Not possible. An enum is not much more than a bunch of compile-time constants. At runtime, they are effectively just ints. The compiler doesn't even check to make sure you are putting valid values in your enum variable (you could put the value 5 in there and it wouldn't mind). So it has no knowledge of what the possible enum values are at runtime.
Of course, if your enum is contiguous (say, you had defined an enum for the values 0, 1, 2 and 3), then you can do this using a for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With