Thread: custom enum class (operator overloading)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    custom enum class (operator overloading)

    Hello

    Someone posted the following code on this forum a while ago:

    Code:
    class state {
    public:
      enum {
         Undefined = 0,
         Arkansas = 1,
         Alaska,
         California
      };
    
    protected:
      int value;
    
    public:
      state() : value(Undefined) { }
      state(int val) : value(val) { }
      state(const state &o) : value(o.value) { }
    
      state operator=(int val) {
         value = val;
      }
    };
    This gives 2 errors: first error is because of const state (3rd contructor) and the second error is that operator= must return a value..

    I would like to be able to do the following with this class:
    Code:
    state s1, s2;
    s1 = state::Alaska;
    s2 = state::California;
    if (s1 == s2) ..
    How should I overload operator= properly in this case?

    Or if I want to do s1 = s2 = s3?

    Thanks a lot for help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This gives 2 errors
    Can you post the errors?

    From the looks of it the s1 = s2 = s3 should work if you return a state reference from operator=, and you just need to overload operator== (or maybe just operator int()) for the other part.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
    'state::value' : cannot convert 'this' pointer from 'const state' to 'state &'
    'state::operator=' : must return a value

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok, so change the return value of the operator= to state&, and return *this from it.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As you know, all functions in C++ that does not have a return type of void must always return something. Just so you know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    As you know, all functions in C++ that does not have a return type of void must always return something. Just so you know.
    I know that but I'm really not familiar with operator overloading.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't worry, operators aren't special They must return something too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    What do I have to do to make this possible:

    Code:
    switch(state) {
      case state::Arizona: ..
    }
    Right now my compiler gives me:
    Code:
    error C2450: switch expression of type 'state' is illegal

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Seems it's trying to switch based upon a state object instead of int...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    Seems it's trying to switch based upon a state object instead of int...
    What should I change?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is the code the same as before or is there an updated code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's parsing like this:
    Code:
    case state:
        :Arizona:
    dunno why.

    try this:
    Code:
    case (state::Arizona) :

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Current code:

    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) { }
    
      state operator=(int val) {
         value = val;
    	 return *this;
      }
    };

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Perhaps provide a get method to return the value, or provide an operator int().

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Daved View Post
    Perhaps provide a get method to return the value, or provide an operator int().
    Well, it would have to be a constant expression, which a get method wouldn't do. I don't know about an operator int(), but I don't think so.

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