//单独形式
x = x + y; x = x - y;
//也可以写为复合形式
x += y; x -= y;
//operator+根据operator+=来实现
const Rational operator+(const Rational& lhs,const Rational& rhs)
{
return Rational(lhs) += rhs;
}
//operator-根据operator-=来实现
const Rational operator-(const Rational& lhs,const Rational& rhs)
{
return Rational(lhs) -= rhs;
}
template<typename T>
const T operator + (const T& lhs,const T& rhs)
{
return T(lhs) += rhs;
}
template<typename T>
const T operator +(const T& lhs,const T& rhs)
{
return T(lhs) += rhs;
//相比于
//T result(lhs);
//return T(lhs) += rhs;
}
operator的复合形式(operator+=)比单独形式(operator+)效率更加高,开发时优先考虑使用复合形式。