Thread: custom enum class (operator overloading)

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Well, it would have to be a constant expression, which a get method wouldn't do.

    The problem is in the switch, not the case. This works fine:
    Code:
    class state {
    public:
      enum {
         Undefined = 0,
         Arkansas = 1,
         Alaska,
         California
      };
    
    protected:
      int value;
    
    public:
      state() : value(Undefined) { }
      state(int val) : value(val) { }
      state(state &o) : value(o.value) { }
    
      int get_state() const { return value; }
      state operator=(int val) {
         value = val;
         return *this;
      }
    };
    
    int main()
    {
        state state1(state::California);
    
        switch (state1.get_state())
        {
        case state::Alaska:
        case state::Arkansas:
        case state::California:
        default:
            break;
        }
    }

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    >> Well, it would have to be a constant expression, which a get method wouldn't do.

    The problem is in the switch, not the case. This works fine:
    Code:
    class state {
    public:
      enum {
         Undefined = 0,
         Arkansas = 1,
         Alaska,
         California
      };
    
    protected:
      int value;
    
    public:
      state() : value(Undefined) { }
      state(int val) : value(val) { }
      state(state &o) : value(o.value) { }
    
      int get_state() const { return value; }
      state operator=(int val) {
         value = val;
         return *this;
      }
    };
    
    int main()
    {
        state state1(state::California);
    
        switch (state1.get_state())
        {
        case state::Alaska:
        case state::Arkansas:
        case state::California:
        default:
            break;
        }
    }
    Thanks Daved! You always find the mistake/solution

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. friends class lil prblm with enum
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2002, 09:57 PM
  5. help on some vector of custom class code
    By cozman in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2001, 11:55 PM