Think_in_CPP第十二章 操作符重载(3)

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对象的析构函数调用



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值