www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Private enum members

reply Kagamin <spam here.lot> writes:
Justin Johansson Wrote:

 enum Color {
   private UNINITIALIZED = -1,
   RED, GREEN, BLUE
 }

It's syntactical ambiguity, I think. There's no much difference between private UNINITIALIZED = -1, RED, GREEN, BLUE and private UNINITIALIZED = -1, RED, GREEN, BLUE
Oct 26 2009
parent reply d-noob <doesnt exist.com> writes:
Kagamin Wrote:

 Justin Johansson Wrote:
 
 enum Color {
   private UNINITIALIZED = -1,
   RED, GREEN, BLUE
 }

It's syntactical ambiguity, I think. There's no much difference between private UNINITIALIZED = -1, RED, GREEN, BLUE and private UNINITIALIZED = -1, RED, GREEN, BLUE

So let's fix it: enum C { private: UNINITIALIZED = -1; public: RED, GREEN, BLUE; } But even now it's easy to set illegal values in client code: C c = RED; c--; // bang! Gotta love the implicit conversions - this is almost like pointer arithmetics
Oct 26 2009
parent Kagamin <spam here.lot> writes:
d-noob Wrote:

 So let's fix it:
 
 enum C {
   private:
   UNINITIALIZED = -1;
   public:
   RED, GREEN, BLUE;
 }

See enums with virtual methods http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
Oct 26 2009