Thread: nested enum operator overloading

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    48

    nested enum operator overloading

    I have an enum nested in an object and I am unsuccessfully trying to increment it to the next value.

    Here's the classes:
    Code:
    class Instruction
    {
    	public:
    		Instruction(uint pc, uint op, int dest, int src1, int src2);
    		~Instruction();
    		enum Stages {IF = 0, ID, IS, EX, WB, RET};
    		Stages stage() const;
    		void incrementStage();
    		bool operator==(const Instruction right);
    		bool operator==( Instruction::Stages &rs );
    		Stages operator++();
    	private:
    		Stages curr_stage;
    };
    And what I think is the closest to correct that I've done:
    Code:
    inline Instruction::Stages operator++(Instruction::Stages rs)
    {
        return (Instruction::Stages)(rs + 1);
    }
    I am attempting to make prefix work but pre or post will be fine.

    Here is the error I am getting: error: no match for ‘operator++’ in ‘++((Instruction*)this)->Instruction::curr_stage’|

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    User supplied operators can only be overloaded to work if one of its operands is a class/struct type. Enums (no matter how nested) are not a class/struct type.

    Operator++ only acts on a class/struct of which it is a member.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Hmm...I see.

    In that case, I could make curr_stage an int and set it equal to a stage and increment will work fine. But how would I then later compare curr_stage with IF, ID or one of the other stages?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I may be mistaken, but it appears to me that you can overload operators for enum types alright:

    Code:
    //as free functions
    Instruction::Stages operator++(Instruction::Stages& n)
    {
        n = Instruction::Stages(n + 1);
        return n;
    }
    
    Instruction::Stages operator++(Instruction::Stages& n, int)
    {
        Instruction::Stages s = n;
        ++n;
        return s;
    }
    In that case, I could make curr_stage an int and set it equal to a stage and increment will work fine. But how would I then later compare curr_stage with IF, ID or one of the other stages?
    Enum types are implicitly converted to integers. Those comparisons just work:

    Code:
    int i = Instructions::IF;
    if (i == Instructions::IF) {}
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 10:29 PM
  2. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM