14.3 Arithmetic Operators and Relational operators
Generally, arithmetic and Relational operators are defined as non-member functions.
To be consistent with the built-in operators, addition returns a right value instead of a reference.
Friend Class1 operator + (const Class1 & c1, const Class1 & c2 );
Class1 operator + (const Class1 & c1, const Class1 & c2)
{
Class1 c (100 );
C. counter = c1.counter + c2.counter;
Return c;
}
Class1 c1 (200 );
Class1 c2 (300 );
Cout <c1 + c2 <endl;
A class that defines both Arithmetic Operators and Related Compound value assignment operators. Generally, compound values must be used to implement arithmetic operators.
14.3.1 equal Operators
Friend bool operator = (const Class1 & c1, const Class1 & c2 );
Inline bool operator = (const Class1 & c1, const Class1 & c2)
{
Return c1.counter = c2.counter;
}
Class1 c1 (200 );
Class1 c2 (300 );
Bool I = c1 = c2;
Cout <I <endl;
Classes with operator = defined are easier to use with the standard library. Some algorithms, such as find, use the = operator by default. If the class defines =, these algorithms can be used for this type without any special processing.
14.3.2 Relational operators
Use the default <operator to associate containers and some algorithms. Generally, Relational operators, such as equal operators, should be defined as non-member functions.
From xufei96's column