Consider a C ++ class that exports an enumeration, maintains an internal array over this enumeration, and wants to export a command that takes values ββfrom an enumeration.
class foo { public: enum color { red, yellow, green, NUM_COLORS }; private: something somebody[NUM_COLORS]; public: void command(color c); };
Is there a clean way to export only actual colors, but not NUM_COLORS? I do not want to check for an edge case on every call when a compiler-type system really needs to be able to do this for me.
Obvious hack:
class foo { public: enum color { red, yellow, green }; private: const unsigned NUM_COLORS = green+1; unsigned LEDs_in_stock[NUM_COLORS]; public: void command(color c); };
This, of course, is a ticking time bomb, expecting some bad overloaded service programmer to add positions for the blue LEDs, and forget to update the NUM_COLORS line.
Let me explain a little. In this particular case, I want to say:
class foo { public: enum color { red, yellow, green }; void command(color c); private: something somebody[color]; };
I understand that C ++ does not allow this.
c ++ visibility arrays enums
John R. Strohm
source share