3

There's a lot of information, examples etc. for operator overloading of all kinds on the web. But I can't for the life of me find an example on how to do this for a simple enum and, say, the |= or the += operators.

For a bitwise or the implementation is this:

  inline MyEnum operator | (MyEnum a, MyEnum b)
  {
    return (MyEnum)((int)a | (int)b);
  }

All the examples I found for compound operations however are for classes; which can easily take the this pointer for the LHS. In an enum I don't have that, so what's the correct syntax?

Update: I have already tried this version:

  inline MyEnum operator |= (MyEnum a, MyEnum b)
  {
    return (MyEnum)((int)a | (int)b);
  }

and it compiles, but doesn't return the correct bitwise or value.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Related post, http://stackoverflow.com/a/4421729/3747990 – Niall Sep 03 '14 at 13:52
  • 2
    You might want to check e.g. [this reference](http://en.cppreference.com/w/cpp/language/operator_assignment). It will tell you that you indeed have the the left hand side of the assignment expression, in the first argument to the function. For more information you might also want [to read this](http://en.cppreference.com/w/cpp/language/operators). – Some programmer dude Sep 03 '14 at 13:52
  • Regarding your update, please read [my linked reference again](http://en.cppreference.com/w/cpp/language/operator_assignment) to see what the arguments and return types are. Hopefully that should tell you more what you need to do. Also think about how you overload the output and input operator, they are kind of similar when it comes to the arguments and what you should return (but do different things of course). – Some programmer dude Sep 03 '14 at 14:06
  • The reference you gave is nice. I searched on cppreference.com myself before but did not find that page. It's only missing one essential thing: an example to see the little detail in the function body. Without the assignment to a the code doesn't compile. – Mike Lischke Sep 04 '14 at 06:52

1 Answers1

5

Based on your update, the implementation and signature needs to be tweaked a little;

inline MyEnum& operator |= (MyEnum& a, MyEnum b)
//           ^ here and           ^ here
{
  return a = (MyEnum)((int)a | (int)b);
}

To get the operation to work as expected, it is important that the signature correlates with the built in ones and it would generally be advised that the implementation correlates as well. Signatures can be obtained from the reference listed in the comments (the canonical in this case is T1& operator |= (T1& lhs, T2 const& rhs).

The references here (MyEnum&) are important (especially for MyEnum& a), such that the operator behaves as a built in one would, and this would be how you would expect it to.

Note the return type; the return type can be any type, even void. This would affect what would be able to be compiled. For example, with the return type above, MyEnum c = (a |= b); would compile. With a void return, it would not, but a |= b; would still compile. It is advised that the return matches the built in ones as this will give you more natural semantics.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • Never woudl have come to the idea to use a reference, but more importantly to use an assignment for the return call (otherwise I get a compiler error). Btw: nice explanation about the return type! – Mike Lischke Sep 04 '14 at 06:55
  • The assignment in the return is really just shorthand, it could also have been `a = (MyEnum)...); return a;` – Niall Sep 04 '14 at 07:12