运算符重载—原型
if it is read-only pass it in as a const reference(except built-ins)
make member functions const that don’t change the calss(boolean operators,+,-,etc)
for global functions, if the left-hand side changes pass as a reference(assignment operators)
Select the return type depending on the expected meaning of the operator.
For example.
—For operator+ you need to generate a new object.
Return as a const object so the result cannot be modified as an value.
—Logical operators should return bool(or int for older compilers)
+-*/%^&|~
—const T operatorX(const T& I, const T& r) const;
! && || < <= == >= >
—bool operatorX(const T& I, const T& r) const;
[]
—T& T::operator[](int index);
operators ++ and –
class Integer
{
public:
…
const Integer& operator++(); //prefix++
const Integer& operator++(int); //postfix++
};