I was reading through some c++ source code, and i came over some syntax.
path& path::operator+=(string postPath)
and i was wondering whether it is actual syntax and why c++ is not using the already existing operator+ instead combined with applying the value to the object in question.
Is it like, if you want to make sure that the object gets deleted properly. But the destructor should deal with all of that.
-Edit1
I know there is a difference between a += b; and a + b;
What i was wondering was why does c++ not just use the operator+ with the += instead of having to redefine the operator+= as the same as the operator+
-Edit2
Im not sure it came across correctly, but what i was asking was why the language doesn't infer += based on +.
And now i realize the other uses of +=. Thanks everyone :)
C++ makes no assumptions with regards to overloaded operators; you can
define an operator+
which is not commutative, for example. And you
can define an operator+=
which has no relation to the operator+
.
You can also define an operator+=
without defining operator+
or vice
versa.
As to why: for starters, you really wouldn't want the compiler to assume
that operator+
on strings is commutative, and optimize in consequence.
And once having opened the door, the authors of the language no doubt
felt that it was best to leave it up to the programmer, rather than to
impose anything at language level. Even if I can't think of a case
off-hand, there may be cases where it makes sense to support +=
and
+
with slightly different semantics. The general philosophy of C++
has always been to give the programmer all of the tools he requires to
write good code, but not to ban code which isn't conform to the current
idea of what is bad code. (In the long run, this has paid off, since
our ideas as to what is good and bad code have evolved.)
Yes, path& path::operator+=(string postPath);
is valid and overloads the +=
operator for the case in which a string
is added onto a path
.
The reason why you want to be able to overload it is simple: operator+
must return a new value instead of changing the old one, while operator+=
can reuse existing resource (e.g. allocated memory). Since the major operation of appending strings (which is what you are doing) is memory allocation, a +=
can be significantly more performant in your exact case.
Additionally, the operation SomePath + SomeString
raises the question of SomeString + SomePath
, and even worse, SomeStringA + SomePathB
, all of which are slightly different, while SomePath += SomeString
has a very clear meaning.
Since it shoud now be clear why you want to be able to implement +=
without implementing +
, consider the other direction: Why not just use a = a + b
as the default implementation for a += b
:
First, the operator overloading rules are older than = delete
. Therefore, this rule would always trigger, even in cases where you do not want operator+=
to exist at all. Note that implementing the "default" is very simple:
A& operator+=(A const& other) { return *this = *this + other; }
why c++ is not using the already existing operator+ instead combined with applying the value
Precisely because the +=
mutates, and +
doesn't. That often implies significant differences in the implementation.
Implementing a += b
as a = a+b
would be less efficient than implementing it directly, since it would have to create and destroy the temporary object that's assigned to a
.
It's more efficient to implement +
in terms of +=
path operator+(path prePath, string postPath) {
return prePath += postPath;
}
Philosophically, one might argue that it's less surprising if the language doesn't magically generate operators that you might not expect. Many people are caught out by implicitly-generated constructors and assignment operators giving their types invalid copy semantics (until they learn the Rule of Three), and other magical behaviour might cause confusion in other ways.