0
嘿,我想知道这两个运营商定义的区别:运算符重载在C++(有和没有朋友的)
1:
class Rational{
//...
public:
//...
Rational operator -() const{ return Rational(-t,b);}
//...
};
2:
class Rational{
//...
public:
//...
friend Rational operator -(const Rational& v) {return Rational(-t,b);}
//...
};
据我了解,用于:
Rational s = -r
r.operator-() // should happen
想要一些explenation的区别,谢谢!
这些声明是否在类中? – 2010-09-30 18:04:42
您的#2甚至不需要成为朋友 - 它不会访问Rational的任何私人成员。它可以(也应该)完全在课堂之外声明/定义。 – 2010-09-30 20:35:24