views:

94

answers:

2

Just curious about how to overload them.

The opAssign operators are like addAssign(+=) and subAssign(-=).

"globally" means they are not overloaded as member functions, but just a operator act on operands

For these opAssign operators, they are binary operators.(they receive two operands) Therefore two parameters are needed.

I found no examples on the web.....

+7  A: 

Here's a trivial example of defining operator+=:

struct Foo{
    int x;
};

Foo& operator+=(Foo& lhs, const Foo& rhs) {
    lhs.x += rhs.x;
    return lhs;
}
sepp2k
rhs should be passed by const reference since rhs isn't modified by the function.
Jon-Eric
+2  A: 

The assignment operator (=) is special in that it always needs to be a non-static member function as per "§13.5.3 Assignment" of the C++ standard.

An assignment operator shall be implemented by a non-static member function with exactly one parameter

The same is true for the function call operator and the subscript operator. Other "assignment" operators (+=, -=, *=, etc) can be free binary functions.

sellibitze
ASFAIU Bossliaw was asking about the operators that combine assignment with some other operation (`+=` etc.), not about pure assignment (`=`).
sbi