Possible Duplicate:
C++ — How to overload operator+=?
I'm thinking about returning a reference to object when overloading += which is return by value in case of '+'?
Possible Duplicate:
C++ — How to overload operator+=?
I'm thinking about returning a reference to object when overloading += which is return by value in case of '+'?
You overload those operators as in:
class X
{
public:
X& operator+=(const X& rhs) { ...; return *this; }
};
X operator+(X lhs, const X& rhs)
{
return lhs += rhs;
}
You can peacefully declare
class Class
{
public:
void operator +=(const Class& obj) ;
} ;
and avoid cryptic code like a += b += c
.