运算符:
+ -
* /
* %
& |
= <
+=
-= *=
/= %=
^= &=
|= <<
………
运算符重载:把运算符的运算,把你写的一个函数去表达出来。故要为写的重载符
operator * (…) 重载*运算符号
const String String::operator + (const String& that);
const String operator+(const String& r, const String& 1);
class Integer
{
public:
Integer(int n = 0):i(n) {}
const Integer operator+(const Integer& n) const
{
return Integer(i+n.i);
}
……
private:
int i;
};
Interger x(1),y(5),z;
x + y ===>x.operator+(y)
const Integer operator-() const
{
return Integer(-i);
}
…….
const Integer operator+(const Integer& rhs, const Integer& 1hs);
Integer x,y;
x + y ===> operator+(x,y);
class Integer
{
friend const Integer operator+ (const Integer& lhs, const Integer& rhs);
……
};
const Integer operator+ (const Integer& lhs, const Integer& rhs)
{
return Integer(lhs.i + rhs.i);
}