custom enum class (operator overloading)

This is a discussion on custom enum class (operator overloading) within the C++ Programming forums, part of the General Programming Boards category; >> Well, it would have to be a constant expression, which a get method wouldn't do. The problem is in ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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
    l2u
    l2u is offline
    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

Page 2 of 2 FirstFirst 12
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, 04:01 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21