C++面向对象编程<三>:操作符重载

操作符重载-成员函数

先看程序

inline complex& 
_doapl(complex* ths, const complex& r) //do assignment plus 
{
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

inline complex& 
complex::operator += (const complex& r)
{
    return _doapl (this,r) ;
}

使用如下

complex c1(2,1);
complex c2(5);

c2 += c1 ;

上面的形式是操作符重载,那么c2 += c1是怎么被编译器看待的?见下

inline complex& 
complex::operator += (this, const complex& r)
{
    return _doapl (this,r) ;
}
//c2相当于this,c1相当于r,相当于c2.operator += (c1);
  1. 所有的成员函数函数都带着一个隐藏的参数this指针。
  2. 为什么重载+=运算符,返回值是complex&而不是void,如对于:c2 += c1 根本不需要知道返回值,但是如果对于:c3 += c2 += c1,这样的形式如果返回值void就会出错了,所以这里的返回值是complex。

操作符重载-非成员函数

先看使用

complex c1(2,1);
complex c2;

c2 = c1 + c2;//两个复数相加
c2 = c1 + 5;//复数加实数
c2 = 7 + c1;//实数加复数

对于上述就有三种使用+=运算符的方法,因此得写三个运算符重载函数。(根据使用方法写函数原型)

inline complex operator + (const complex& x, const complex& y)
{
    return complex (real (x) + real (y), imag (x) + imag (y) );
}

inline complex operator + (const complex& x, double y)
{
    return complex (real (x) + y, imag (x) );
}

inline complex operator + (const complex& x, const complex& y)
{
    return complex (x + real (y), imag (y) );
}

上诉代码是从stl中拿出来的一部分源码。
为什么把+重载函数设置为全局函数,而不成员函数?若是+重载函数为全局函数,就没法考虑实数+复数和复数+实数的情况了,而只能考虑复数加实数的情况。若这样做如下

inline complex& complex::operator + (double y);
c1 + 3;  //实际上如下:c1.operator+(3),但却不能实现3.operator+(c1)
  • temp object临时对象
    typename()就是要创建临时对象,对于临时对象的生命周期:下一行就结束了。创建临时对象的用法在创建stl中很常见。
    注意对于这些临时对象绝不可return by reference。因为它们是local object。

  • 正号和负号的重载
    使用如下

complex c1(2,1);
comlex c2;
cout << -c1;
cout << +c1;

其重载函数如下

inline complex operator + (const complex& x)
{
    return x;
}

inline complex operator - (const complex& x)
{
    return complex (-real (x), -imag (x) );
}

正号和加号的重载函数,负号于减号重载函数,怎么区分呢?靠参数的个数来区分不同的重载函数。

  • ==符号的重载
    使用如下:
complex c1(2,1);
comlex c2;

cout << (c1 == c2);
cout << (c1 == 2);
cout << (0 == c2);

对应的有三种重载函数:

inline bool operator == (const complex& x, const complex& y)
{
    return real (x) == real (y) && imag(x) == imag(y);
}

inline bool operator == (const complex& x, double y)
{
    return real (x) == y && imag(x) == 0;
}

inline bool operator == (double x, const complex& y)
{
    return x == real (y) && imag(y) == 0;
}
  • !=运算符的重载
    使用如下:
complex c1(2,1);
comlex c2;

cout << (c1 != c2);
cout << (c1 != 2);
cout << (0 != c2);

重载函数如下:

inline bool operator != (const complex& x, const complex& y)
{
    return real (x) != real (y) || imag(x) != imag(y);
}

inline bool operator != (const complex& x, double y)
{
    return real (x) != y || imag(x) != 0;
}

inline bool operator != (double x, const complex& y)
{
    return x != real (y) || imag(y) != 0;
}
  • <<运符号的重载
    使用如下:
complex c1(2,1)
cout << c1 << conj(c1)
/*
1.cout << c1左边是cout,右边是c1,因此可以决定<<重载函数参数的第一个参数(左边)和第二个参数(右边)的类型
2.先输出c1,得到得结果还要继续能够接受conj(c1)
*/

重载函数如下:

inline complex conj (const complex& x)  // 共轭复数
{
    return complex(real(x), -imag(x) );
}

ostream& operator << (ostream& os, const complex& x)
{
  return os << '(' << real (x) << ',' << imag (x) << ')';
}
/*可以看出cout是个对象,其类型是ostream。*/

但若这样写重载函数

void operator << (ostream& os, const complex& x)
{
  return os << '(' << real (x) << ',' << imag (x) << ')';
}

就不能像下面这样用了

complex c1(2,1)
cout << c1 << conj(c1)  //此句会出错

操作符的重载一定是作用在左边操作数的。会把<<符号作用在左边身上。不要把<<操作符写成成员函数,一定要写成全局的函数。解释下:

//若把<<重载为成员函数,那就得这样使用,显然不会这样使用
complex& complex:: operator << (ostream& os)
c1 << cout;
  • 小节
    1. 函数:任何一个操作都有两种写法:全局函数或成员函数。
    2. 传递参数:尽量用 pass by reference,若是不用改变参数,则加上const
    3. 传递返回值:若不是local object,经量用return by reference
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值