How to overload the ++ operator for a enum in C++
Asked Answered
R

3

6

This is what I tried, but I see that overloading only increments the variable if I assign it to another variable. I.e, The value of the variable on which I do the increment does not increase. So, in the example below variable newDay is THU but currentDay remains WED.

How do I define the overload operator to increment variable I am doing the operation on?

typedef enum days {MON, TUE, WED, THU, FRI, SAT, SUN} days;

inline days operator++ (days& d) {
    return static_cast<days>((static_cast<int>(d) + 1) % 7);
}

int main(int argc, const char * argv[]) {
   days currentDay = WED;
   days newDay = ++currentDay;
   cout << "Current day: " << currentDay << ", Stored day: " << calendar[0] << ", New day: " << newDay << endl;
}
Recapitulation answered 23/7, 2017 at 6:16 Comment(4)
Why do you expect static_cast<int>(d) + 1) % 7 to increment something, and what it that something?Coumarone
typedef enum days {...} days; is an exercise in redundancy. Just define enum days {...};. C++ doesn't require the same acrobatics as C to introduce the enum as a type.Pentecostal
Consider using scoped enum.Imbibe
I would not recommend you override operator++ to cycle. Your app will hang if you do a for loop like for( days d = MON; d <= SUN; d++ ). You should use a C++ function for something like that instead.Polk
P
3

If I modify your overloaded operator to this:

inline days operator++ (days const& d) {
    return static_cast<days>((static_cast<int>(d) + 1) % 7);
}

It still compiles, despite the fact I added a const specifier there. That's because you are not modifying d like the semantics of prefix ++ demand.

So make sure you do modify it, if you want the operator to have the desired effect:

inline days operator++ (days& d) {
    d = static_cast<days>((static_cast<int>(d) + 1) % 7);
    return d;
}

Without commenting on the validity of your own design, note that it is a widely held opinion that prefix operator++ should return a modifiable lvalue, like the built-ins do. Bear in mind if you find yourself writing code like ++x = y, you need to return a reference, i.e. date& operator++(date&).

Pentecostal answered 23/7, 2017 at 6:28 Comment(1)
Just be aware that the modulo operator is very slow. Increasing the value and comparing to the maximum is much faster.Illconsidered
P
0

First, I wouldn't recommend you overload operator++ to have the enum cycle.

This could easily cause a hang if you have a loop like:

for( days d = MON; d <= SUN; d++ ) {
  // hangs
}

It's really unexpected in C++ that operator++ cycles. Consider using a C++ function like this, so it's 100% clear from the code that the enum will cycle

template <typename E>
inline E& cycleEnum( E &val, E minVal, E maxVal ) {
  int e = (int)val;
  if( ++e > maxVal )
    e = minVal;
  return val = (E)e;
}

Use:

days day = MON;
cycleEnum( day, MON, SUN );

Defining operator++ for enums

I have seen this done with a macro before

This only defines pre & post increment, but you could define bitwise ops too (though you probably shouldn't define increment and bitwise on the same enum class)

#include <map>
#include <string>
using std::map, std::string;

enum class MouseButton {
  Left,
  Middle,
  Right,

  NUM_BUTTONS
};

#define PRE_AND_POST_INCREMENT( ENUM )                        \
inline ENUM& operator++( ENUM& enumVal ) {                    \
  return (ENUM&)(++(int&)enumVal);                            \
}                                                             \
                                                              \
inline ENUM operator++( ENUM& enumVal, int postIncrement ) {  \
  ENUM oldValue = enumVal;                                    \
  ++enumVal;                                                  \
  return oldValue;                                            \
}                                                             \
  
PRE_AND_POST_INCREMENT( MouseButton )

map< MouseButton, string > mouseButtonName = {
  { MouseButton::Left, "Left" },
  { MouseButton::Middle, "Middle" },
  { MouseButton::Right, "Right" },
};

int main() {

  for( MouseButton mb = MouseButton::Left; mb < MouseButton::NUM_BUTTONS; mb++ ) {
    puts( mouseButtonName[ mb ].c_str() );
  }
  
}

There is also this template approach which you could adapt for operator++ (warning: it modifies all enums to act like an int!)

Polk answered 27/4 at 4:56 Comment(0)
O
-1

You defined the postfix operator. The normal behavior of the postfix operator is to increment the value of its argument but return the original value. It should behave like this:

days operator++(days& d,int){
    days temp=d;
    d=static_cast<days>((static_cast<int>(d) + 1) % 7);
    return temp;
}

What you want is the prefix operator. This increments the value of the argument and returns a reference to its arguments. It should look like this:

days& operator++(days& d){
    d=static_cast<days>((static_cast<int>(d) + 1) % 7);
    return d;
}
Oujda answered 23/7, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.