More generally, what operators should I be overloading? I know that an overloaded +=
is a good thing because the expansion to a = a + b
creates a temporary object, whereas an overloaded +=
can avoid that. Will a +=
that is not overloaded effectively use my overloaded operators?

- 601,492
- 42
- 1,072
- 1,490

- 10,924
- 9
- 52
- 71
-
Surely modern compilers (and even quite venerable ones) treat `a += b` identically to `a = a + b`? – David Heffernan Jan 17 '11 at 16:01
-
1For the built-in types, yes, @David, they probably do. But they're still three distinct and independent operators. – Rob Kennedy Jan 17 '11 at 16:03
-
See rule #3 in the [The Three Basic Rules of Operator Overloading in C++](http://stackoverflow.com/questions/4421706/operator-overloading/4421708#4421708). – sbi Apr 04 '11 at 08:04
8 Answers
If you don't overload +=
it won't be defined; you won't be able to use it.
I'd personally suggest you to provide it, since whoever would probably expect that +=
is available, if +
and =
are.
I'd also suggest you to use Boost.Operators in order to overload the operators that should be automatically generated in terms of others.

- 161,384
- 21
- 275
- 467

- 25,562
- 20
- 98
- 150
-
+1 for boost operators (not the first time I think, but it's always a good recommendation :) ) – Matthieu M. Jan 17 '11 at 16:04
-
Eheh, yup, I think [this has happened before](http://stackoverflow.com/questions/4667499/should-operator-always-be-implemented-via-operator-in-c/4667587#4667587). – peoro Jan 17 '11 at 16:07
You don't have to. The compiler won't throw an error or warning telling you that what you've done is wrong.
However, you really really should in the vast majority of cases. The compiler will NOT transform a += b
into a = a + b
.

- 144,682
- 38
- 256
- 465
The best way to implement operator+
, favoring code reuse, is
class Foo
{
Foo(Foo const&); // Implemented
Foo& operator+=(Foo const& rhs)
{
// Implement all the addition-specific code here
return *this;
}
};
and then
Foo operator+(Foo x, Foo const& y)
{
x += y; return x;
}
If you have to do a copy of the right hand side (for implementation specific reasons, it is usually not the case), you can pass by value in both operator+=
and the second argument of operator+
.

- 55,948
- 11
- 128
- 197
-
2@There is nothing we can do: I'm not sure all compilers will correctly do the RVO in this case, since `operator+=` returns a reference. And also, you don't need parentheses: `return x += y;` – Alexandre C. Jan 17 '11 at 16:49
Yes, it's a different operator, so you will have to overload it. You can (and probably should) use your other operators (e.g. '+') inside the overloaded function.

- 512
- 1
- 8
- 19
It is better to implement operator+=
and operator=
first, as implementing operator+
in terms of them is trvial. IIRC this is in Effective C++ by Scott Meyers.

- 46,613
- 43
- 151
- 237
-
2+1, the conventional way is as a free function: `type operator+( type lhs, type const & rhs ) { return lhs += rhs; }`. – David Rodríguez - dribeas Jan 17 '11 at 17:14
You have to overload +=.
Then, you could overload it to do something completely different, but it is not advised. ;-).

- 38,876
- 35
- 121
- 169
-
Please beware of one-word answers. The question in the title is the opposite of the question in the body, so it's not clear which one you're answering *no* to. – Rob Kennedy Jan 17 '11 at 16:04
When you say "do I have to..", only if you need to use it. It is better generally to implement operator+= and then implement operator+ in terms of it, because most people get operator+ wrong (don't make it const, forget to return a copy...)
std::accumulate uses a = a + b and if all you are doing is writing something to fit it, += won't help on its own, although you can still then implement operator+ in terms of it.
Of course you might decide you don't want to allow users to modify your objects with += (whereas you don't mind them making copies).

- 30,981
- 5
- 61
- 92
No you DO NOT HAVE TO. But just don't think that because you overloaded + and = you have += for free. Shurely you do not assume that just because you've overloaded - and > you also have -> operator?

- 23,727
- 30
- 106
- 194
-
4This doesn't seem like a useful comment. `+=`, `+`, and `=` are related in a way that `-`, `>`, and `->` aren't. – David Thornley Jan 17 '11 at 16:09
-
1@David and DeadMG and where is your sense of humor? As a matter of fact I'm sure he will remember this funny example better than dry explanation. – There is nothing we can do Jan 17 '11 at 16:33