您的位置:首页 > 编程语言 > C语言/C++

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

2016-02-21 23:21 489 查看

操作符重载-成员函数

先看程序

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);


所有的成员函数函数都带着一个隐藏的参数this指针。

为什么重载+=运算符,返回值是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;


小节

函数:任何一个操作都有两种写法:全局函数或成员函数。

传递参数:尽量用 pass by reference,若是不用改变参数,则加上const

传递返回值:若不是local object,经量用return by reference
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: