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; Hello Someone posted the following code on this forum a while ago: Code: class state { public: enum { Undefined ...

  1. #1
    l2u
    l2u is offline
    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,317
    >> 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
    l2u
    l2u is offline
    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,317
    Ok, so change the return value of the operator= to state&, and return *this from it.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    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
    l2u
    l2u is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    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
    l2u
    l2u is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    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
    l2u
    l2u is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    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
    l2u
    l2u is offline
    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,317
    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.

Page 1 of 2 12 LastLast
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