If I overloaded the + and = operators, do I need to overload + =?

In general, which operators need to be overloaded? I know that overloaded +=is good, because expanding to a = a + bcreates a temporary object, while overloaded +=can avoid this. Would +=that which is not overloaded use my overloaded operators efficiently?

+3
source share
8 answers

The best way to implement operator+, preferring 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 need to make a copy of the right side (this is usually not the case for specific implementation purposes), you can pass the value in both the operator+=second argument operator+.

+5

+=, ; .

, , , , , += , + =.

Boost.Operators, , .

+5

. , , .

, . a += b a = a + b.

+3

operator+= operator= -, operator+ trvial. IIRC ++ .

+3

, , . (, "+" ) .

+2

+ =.

, - , .; -.)

+1

" ...", . operator + =, + , + ( const, ...)

std:: accumulate a = a + b, , , - , + = , + .

, , , + = ( ).

0

No, you DO NOT. But just don’t think that, because you are overloaded + and = you have + = free. Of course, you don’t assume that just because you overloaded - and> do you also have a → operator?

-4
source

Source: https://habr.com/ru/post/1785937/


All Articles