12.4 操作符重载中的参数和返回值
12.4.1 单目运算符
全局函数方式
const Integer& operator+(const Integer& a);
const Integer operator-(const Integer& a);
const Integer operator~(const Integer& a);
Integer* operator&(Integer& a);
int operator!(const Integer& a);
const Integer& operator++(Integer& a);
const Integer operator++(Integer& a, int);
const Integer& operator--(Integer& a);
const Integer operator--(Integer& a, int);
成员函数方式
class Byte {
unsigned char b;
public:
Byte(unsigned char bb = 0) : b(bb) {}
const Byte& operator+() const;
const Byte operator-() const;
const Byte operator~() const;
Byte operator!() const;
Byte* operator&();
const Byte& operator++();
const Byte operator++(int);
const Byte& operator--();
const Byte operator--(int)
};
12.4.2 双目运算符
全局函数方式
const Integer operator+(const Integer& left, const Integer& right);
const Integer operator-(const Integer& left, const Integer& right);
const Integer operator*(const Integer& left, const Integer& right);
const Integer operator/(const Integer& left, const Integer& right);
const Integer operator%(const Integer& left, const Integer& right);
const Integer operator^(const Integer& left, const Integer& right);
const Integer operator&(const Integer& left, const Integer& right);
const Integer operator|(const Integer& left, const Integer& right);
const Integer operator<<(const Integer& left, const Integer& right);
const Integer operator>>(const Integer& left, const Integer& right);
Integer& operator+=(Integer& left, const Integer& right);
Integer& operator-=(Integer& left, const Integer& right);
Integer& operator*=(Integer& left, const Integer& right);
Integer& operator/=(Integer& left, const Integer& right);
Integer& operator%=(Integer& left, const Integer& right);
Integer& operator^=(Integer& left, const Integer& right);
Integer& operator&=(Integer& left, const Integer& right);
Integer& operator|=(Integer& left, const Integer& right);
Integer& operator>>=(Integer& left, const Integer& right);
Integer& operator<<=(Integer& left, const Integer& right);
int operator==(const Integer& left, const Integer& right);
int operator!=(const Integer& left, const Integer& right);
int operator<(const Integer& left, const Integer& right);
int operator>(const Integer& left, const Integer& right);
int operator<=(const Integer& left, const Integer& right);
int operator>=(const Integer& left, const Integer& right);
int operator&&(const Integer& left, const Integer& right);
int operator||(const Integer& left, const Integer& right);
成员函数方式
class Byte {
unsigned char b;
public:
Byte(unsigned char bb = 0) : b(bb) {}
const Byte
operator+(const Byte& right) const
const Byte
operator-(const Byte& right) const
const Byte
operator*(const Byte& right) const
const Byte
operator/(const Byte& right) const
const Byte
operator%(const Byte& right) const
const Byte
operator^(const Byte& right) const
const Byte
operator&(const Byte& right) const
const Byte
operator|(const Byte& right) const
const Byte
operator<<(const Byte& right) const
const Byte
operator>>(const Byte& right) const
Byte& operator=(const Byte& right)
Byte& operator+=(const Byte& right)
Byte& operator-=(const Byte& right)
Byte& operator*=(const Byte& right)
Byte& operator/=(const Byte& right)
Byte& operator%=(const Byte& right)
Byte& operator^=(const Byte& right)
Byte& operator&=(const Byte& right)
Byte& operator|=(const Byte& right)
Byte& operator>>=(const Byte& right)
Byte& operator<<=(const Byte& right)
int operator==(const Byte& right) const
int operator!=(const Byte& right) const
int operator<(const Byte& right) const
int operator>(const Byte& right) const
int operator<=(const Byte& right) const
int operator>=(const Byte& right) const
int operator&&(const Byte& right) const
int operator||(const Byte& right) const
};
12.4.3 要点备忘
1)在函数中不需要修改,设置为const。比如+/-等算术操作符和Boolean。赋值操作符如+=和=会修改左值,因此代表左值的参数不能是const
2)返回值的类型取决于对操作符作用的期望。如果操作符是要生成一个新对象,则重载函数应该产生一个新对象并以值的方式返回
3)赋值操作符会修改左值,为了实现链式赋值,一般重载函数返回左值的引用。引用是const与否,取决于需求。a=b=c中并不要求返回的引用必须为非const,(a=b).func()中则希望返回一个非const的引用。
4)对于逻辑操作符,最坏返回int,最好是bool类型
++/--的前置版本和后置版本
不管是前置版本还是后置版本,都会修改对象,所以参数不能为const。后置版本返回的是一个包含原来值的新对象。此新对象是以值的方式返回的,且为const。这些也不是硬性规定(也只是一般情况下),也可以返回为int/bool以适应某些特殊的需求。
考虑如下两种返回方式:
return Integer(left.i + right.i);
1)直接在返回处建立临时对象,并调用普通的构造函数
2)没有析构函数
Integer tmp(left.i + right.i);
return tmp;
1)建立tmp对象,调用构造函数
2)copy构造函数复制tmp到返回值对象处
3)tmp对象的析构函数调用