class Rational{ public: Rational(int numerator=0,int denominator=1); friend
const Ration operator*(const Rational& lhs,const Rational& rhs); private: int
n,d; };

const Rational& operaotr*(const Rational& lhs,const Rational& rhs) { Rational
result(lhs.n*rhs.n,lhs.d*rhs.d); return result; }

const Rational& operator*(const Rational& lhs,const Rational& rhs) { Rational*
result=new Rational(lhs.n*rhs.n,lhs.d*rhs.d); return *result; }

Rational w,x,y,z; w=x*y*z;//与operator*(operator*(x,y),z)相同

const Rational& operator*(const Rational& lhs,const Rational& rhs) { static
Rational result;//static对象，此函数将返回其reference。 //result=...
//将lhs乘以rhs，并将结果置于result内 return result; }

bool operator==(const Rational& lhs,const Rational& rhs); Rational a,b,c,d;
if((a*b)==(c*d)) { //... } else { //... }

if(operator==(operator*(a,b),operator*(c,d)));

Rational对象。因此operator==被要求将“operator* 内的static Rational对象值”拿来和“operator*

inline const Rational operator*(const Rational& lhs,const Rational& rhs) {
return Rational(lhs.n*rhs.n,lhs.d*rhs.d); }

stack对象，或返回reference指向一个heap-allocated对象，或返回pointer或reference指向一个local
static对象而有可能同时需要多个这样的对象。

GitHub

Gitee