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

C++运算符重载

2014-04-21 15:39 330 查看
在前一节中曾提到过,C++中运行时的多态性主要是通过虚函数来实现的,而编译时的多态性是由函数重载和运算符重载来实现的。这一系列我将主要讲解C++中有关运算符重载方面的内容。在每一个系列讲解之前,都会有它的一些基础知识需要我们去理解。而运算符重载的基础就是运算符重载函数。所以今天主要讲的是运算符重载函数。

  1.运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生。比如

1 int i;
2 int i1=10,i2=10;
3 i=i1+i2;
4 std::cout<<"i1+i2="<<i<<std::endl;
5
6 double d;
7 double d1=20,d2=20;
8 d=d1+d2;
9 std::cout<<"d1+d2="<<d<<std::endl;


在这个程序里"+"既完成两个整形数的加法运算,又完成了双精度型的加法运算。为什么同一个运算符"+"可以用于完成不同类型的数据的加法运算?这是因为C++针对预定义基本数据类型已经对"+"运算符做了适当的重载。在编译程序编译不同类型数据的加法表达式时,会自动调用相应类型的加法运算符重载函数。但是C++中所提供的预定义的基本数据类型毕竟是有限的,在解决一些实际的问题时,往往需要用户自定义数据类型。比如高中数学里所提到的复数:

1 class Complex //复数类
2 {
3 public:
4 double real;//实数
5 double imag;//虚数
6         Complex(double real=0,double imag=0)
7         {
8 this->real=real;
9 this->imag=imag;
10         }
11 }


假如我们建立两个复数,并用"+"运算符让它们直接相加:

1 Complex com1(10,10),com2(20,20),sum;
2 sum=com1+com2;


那么会提示没有与这些操作数匹配的 "+" 运算符的错误。这是因为Complex类类型不是预定义类型,系统没用对该类型的数据进行加法运算符函数的重载。C++就为运算符重载提供了一种方法,即运算符重载函数。其函数名字规定为operator后紧跟重载运算符。比如:operator+(),operator*()等。现在我们给上述程序声明一个加法运算符的重载函数用于完成复数的加法运算:



1 #include "stdafx.h"
2 #include <iostream>
3
4 class Complex //复数类
5 {
6 public:
7 double real;//实数
8 double imag;//虚数
9         Complex(double real=0,double imag=0)
10         {
11 this->real=real;
12 this->imag=imag;
13         }
14 };
15
16 Complex operator+(Complex com1,Complex com2)//运算符重载函数
17 {
18 return Complex(com1.real+com2.real,com1.imag+com2.imag);
19 }
20
21 int main()
22 {
23     Complex com1(10,10),com2(20,20),sum;
24     sum=com1+com2;//或sum=operator+(com1,com2)
25
26     std::cout<<"sum的实数部分为"<<sum.real<<std::endl;
27     std::cout<<"sum的虚数部分为"<<sum.imag<<"i"<<std::endl;
28
29 return0;
30 }


结果:



在上述示例代码中,调用运算符重载函数时,也可以以operator+(com1,com2)的形式来调用,实际上com1+com2在程序解释时也是转化成前者一样的形式。但是直接用com1+com2的形式更加符合人的书写习惯。

  2.上述示例中的运算符重载函数是不属于任何的类,是全局的函数。因为在Complex类(复数类)中的数据成员是公有的性质,所以运算符重载函数可以访问。但如果定义为私有的呢,那该怎么办。其实,在实际的运算符重载函数声明当中,要不定义其为要操作类的成员函数或类的友元函数。

  (1)运算符重载函数作为类的友元函数的形式:

  class 类名

  {

    friend 返回类型 operator运算符(形参表);

  }

  类外定义格式:

  返回类型 operator运算符(参数表)

  {

    函数体

  }

友元函数重载双目运算符(有两个操作数,通常在运算符的左右两则),参数表中的个数为两个。若是重载单目运算符(只有一个操作数),则参数表中只有一参数。

i.友元函数重载双目运算符(+):



1 #include "stdafx.h"
2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10         Complex(double real=0,double imag=0)
11         {
12 this->real=real;
13 this->imag=imag;
14         }
15         friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+
16 void showSum();
17 };
18
19
20 Complex operator+(Complex com1,Complex com2)//友元运算符重载函数
21 {
22 return Complex(com1.real+com2.real,com1.imag+com2.imag);
23 }
24
25 void Complex::showSum()
26 {
27     std::cout<<real;
28 if(imag>0)
29         std::cout<<"+";
30 if(imag!=0)
31         std::cout<<imag<<"i"<<std::endl;
32 }
33
34 int main()
35 {
36     Complex com1(10,10),com2(20,-20),sum;
37     sum=com1+com2;//或sum=operator+(com1,com2)
38     sum.showSum();//输出复数相加结果
39
40 return0;
41 }


结果:



ii.友元函数重载单目运算符(++):



1 #include "stdafx.h"
2 #include <iostream>
3
4 class Point//坐标类
5 {
6 private:
7 int x;
8 int y;
9 public:
10     Point(int x,int y)
11     {
12 this->x=x;
13 this->y=y;
14     }
15     friend voidoperator++(Point& point);//友元函数重载单目运算符++
16 void showPoint();
17 };
18
19 voidoperator++(Point& point)//友元运算符重载函数
20 {
21 ++point.x;
22 ++point.y;
23 }
24
25 void Point::showPoint()
26 {
27     std::cout<<"("<<x<<","<<y<<")"<<std::endl;
28 }
29
30 int main()
31 {
32     Point point(10,10);
33 ++point;//或operator++(point)
34     point.showPoint();//输出坐标值
35
36 return0;
37 }


结果:



运算符重载函数可以返回任何类型,甚至是void,但通常返回类型都与它所操作的类类型一样,这样可以使运算符使用在复杂的表达式中。比如把上述双目运算符重载函数示例代码中main()主函数里的com1+com2改为com1+com2+com2,那么结果又会不一样了。像赋值运算符=、下标运算符[]、函数调用运算符()等是不能被定义为友元运算符重载函数。同一个运算符可以定义多个运算符重载函数来进行不同的操作。

  (2)运算符重载函数作为类的成员函数的形式:

  class 类名

  {

    返回类型 operator 运算符(形参表);

  }

   类外定义格式:

  返回类型 类名:: operator 运算符(形参表)

  {

    函数体;

  }

对于成员函数重载运算符而言,双目运算符的参数表中仅有一个参数,而单目则无参数。同样的是重载,为什么和友元函数在参数的个数上会有所区别的。原因在于友元函数,没有this指针。

i.成员函数重载双目运算符(+):



1 #include "stdafx.h"
2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10         Complex(double real=0,double imag=0)
11         {
12 this->real=real;
13 this->imag=imag;
14         }
15         Complex operator+(Complex com1);//成员函数重载双目运算符+
16 void showSum();
17 };
18
19
20 Complex Complex::operator+(Complex com1)
21 {
22 return Complex(real+com1.real,imag+com1.imag);
23 }
24
25 void Complex::showSum()
26 {
27     std::cout<<real;
28 if(imag>0)
29         std::cout<<"+";
30 if(imag!=0)
31         std::cout<<imag<<"i"<<std::endl;
32 }
33
34
35 int main()
36 {
37     Complex com1(10,10),com2(20,-20),sum;
38     sum=com1+com2;//或sum=com1.operator+(com2)
39     sum.showSum();//输出复数相加结果
40 return0;
41 }


对于双目运算符而言,运算符重载函数的形参中仅为一个参数,它作为运算符的右操作数(如com2对象),而当前对象作为左操作数(如:上述中的com1对象),它是通过this指针隐含传递给成员运算符重载函数的。

ii.成员函数重载单目运算符(++):



1 #include "stdafx.h"
2 #include <iostream>
3
4
5 class Point//坐标类
6 {
7 private:
8 int x;
9 int y;
10 public:
11     Point(int x,int y)
12     {
13 this->x=x;
14 this->y=y;
15     }
16 voidoperator++();//成员函数重载双目运算符++
17 void showPoint();
18 };
19
20
21 void Point::operator++()
22 {
23 ++x;
24 ++y;
25 }
26
27
28 void Point::showPoint()
29 {
30     std::cout<<"("<<x<<","<<y<<")"<<std::endl;
31 }
32
33 int main()
34 {
35     Point point(10,10);
36 ++point;//或point.operator++()
37     point.showPoint();//输出坐标值
38
39 return0;
40 }


对于单目运算符而言,当前对象作为运算符的操作数。

  在运算符重载运用时应该注意以下几个问题:(1)C++中只能对已有的C++运算符进行重载,不允许用户自己定义新的运算符;(2)C++中绝大部分的运算符可重载,除了成员访问运算符.,成员指针访问运算符.*,作用域运算符::,长度运算符sizeof以及条件运算符?:;(3)重载后不能改变运算符的操作对象(操作数)的个数。如:"+"是实现两个操作数的运算符,重载后仍然为双目运算符;(4)重载不能改变运算符原有的优先级;(5)重载不能改变运算符原有结合的特性。比如:z=x/y*a,执行时是先做左结合的运算x/y,重载后也是如此,不会变成先做右结合y*a;(6)运算符重载不能全部是C++中预定义的基本数据,这样做的目的是为了防止用户修改用于基本类型数据的运算符性质;(7)从上述的示例中可以看到双目运算符可以被重载为友元函数也可以重载为成员函数,但有一种情况,只能使用友元函数,是什么情况呢?我举个例子: 

1 class Complex //复数类
2 {
3 private://私有
4 double real;//实数
5 double imag;//虚数
6 public:
7         Complex(double real=0,double imag=0)
8         {
9 this->real=real;
10 this->imag=imag;
11         }
12         Complex operator+(int x);
13 };
14
15 Complex Complex::operator+(int x)
16 {
17 return Complex(real+x,imag);
18 }
19
20 int main()
21 {
22     Complex com1(5,10),total;
23     total=com1+5;
24
25 return0;
26 }


如果我们把上述main()主函数实现部分里的total=com1+5改为total=5+com1;那么程序就会报错(没有与这些操作数匹配的 "+" 运算符),因为左操作数5不是该复数类的对象,不能调用相应的成员函数Complex operator+(int x),所以编译错误。但如果我们定义一下两个友元函数就能解决上述的问题:

  friend Complex operator+(Complex com1,int x);

  friend Complex operator+(int x,Complex com1);

  3.最后还是一样,我将用一个示例来总结一下今天所讲的内容(开发工具:vs2010): 



1 #include "stdafx.h"
2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10         Complex(double real=0,double imag=0)
11         {
12 this->real=real;
13 this->imag=imag;
14         }
15         Complex operator+(Complex com1);//成员函数重载双目运算符+
16 //或friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+
17         friend Complex operator+(Complex com1,int x);//友元函数重载双目运算符+
18 //或Complex operator+(int x);
19         friend Complex operator+(int x,Complex com1);//友元函数重载双目运算符+
20 void showSum();
21 };
22
23
24 Complex Complex::operator+(Complex com1)
25 {
26 return Complex(real+com1.real,imag+com1.imag);
27 }
28
29 Complex operator+(Complex com1,int x)//左操作数类型为复数,右操作数的类型为整数
30 {
31 return Complex(com1.real+x,com1.imag);
32 }
33
34 Complex operator+(int x,Complex com1)//左操作数类型为整数,右操作数的类型为复数
35 {
36 return Complex(x+com1.real,com1.imag);
37 }
38
39 void Complex::showSum()
40 {
41     std::cout<<real;
42 if(imag>0)
43         std::cout<<"+";
44 if(imag!=0)
45         std::cout<<imag<<"i"<<std::endl;
46 }
47
48 class Point//坐标类
49 {
50 private:
51 int x;
52 int y;
53 public:
54     Point(int x,int y)
55     {
56 this->x=x;
57 this->y=y;
58     }
59     friend voidoperator++(Point& point);//友元函数重载单目运算符++
60     Point operator++();//成员函数重载双目运算符++
61 void showPoint();
62 };
63
64 voidoperator++(Point& point)//友元运算符重载函数
65 {
66 ++point.x;
67 ++point.y;
68 }
69
70 Point Point::operator++()
71 {
72 ++x;
73 ++y;
74 return*this;//返回当前对象
75 }
76
77
78 void Point::showPoint()
79 {
80     std::cout<<"("<<x<<","<<y<<")"<<std::endl;
81 }
82
83 int main()
84 {
85 //两个复数相加
86     std::cout<<"两个复数相加:"<<std::endl;
87
88     Complex com1(10,10),com2(20,-20),sum;
89     sum=com1+com2;//或sum=com1.operator+(com2)
90     std::cout<<"(10+10i)+(20-20i)=";
91     sum.showSum();//输出复数相加结果
92
93 //三个复数相加
94     std::cout<<"三个复数相加:"<<std::endl;
95
96     sum=com1+com2+com2;
97     std::cout<<"(10+10i)+(20-20i)+(20-20i)=";
98     sum.showSum();
99
100 //整数和复数相加
101     std::cout<<"整数和复数相加:"<<std::endl;
102
103     Complex com3(5,10),total;
104     total=com3+5;//或total=operator+(com1,5);
105     std::cout<<"(5+10i)+5=";
106     total.showSum();
107
108     total=5+com3;//或total=operator+(5,com1);
109 //只能用友元函数来重载运算符
110     std::cout<<"5+(5+10i)=";
111     total.showSum();
112
113 //单目运算符++重载
114     std::cout<<"单目运算符++重载:"<<std::endl;
115
116 //注意:下述实现部分不能只用一个++point会造成二义性
117     Point point(10,10);
118 //调用友元函数
119 operator++(point);//或++point
120     std::cout<<"调用友元函数:++(10,10)=";
121     point.showPoint();//输出坐标值
122
123 //调用成员函数
124     point=point.operator++();//或++point;
125     std::cout<<"调用成员函数:++(10,10)=";
126     point.showPoint();
127
128 return0;
129 }


结果:



上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题。++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又是完全不同的(q前置运算符:先加1,再赋值;后置运算符:先赋值,再加1)。那么要怎么重载它们,才可以有效的区分开来呢?今天我就来说说C++中是怎么处理前置运算符和后置运算符的重载的。以及介绍一下插入运算符(>>)和提取运算符(<<)的重载。

  1.在C++里编译器是根据运算符重载函数参数表里是否插入关键字int来区分前置还是后置运算。比如:

1 #include "stdafx.h"
2 #include <iostream>
3
4 class TDPoint//三维坐标
5 {
6 private:
7 int x;
8 int y;
9 int z;
10 public:
11         TDPoint(int x=0,int y=0,int z=0)
12         {
13 this->x=x;
14 this->y=y;
15 this->z=z;
16         }
17         TDPoint operator++();//成员函数重载前置运算符++
18         TDPoint operator++(int);//成员函数重载后置运算符++
19         friend TDPoint operator++(TDPoint& point);//友元函数重载前置运算符++
20         friend TDPoint operator++(TDPoint& point,int);//友元函数重载后置运算符++
21 void showPoint();
22 };
23
24 TDPoint TDPoint::operator++()
25 {
26 ++this->x;
27 ++this->y;
28 ++this->z;
29 return*this;//返回自增后的对象
30 }
31
32 TDPoint TDPoint::operator++(int)
33 {
34     TDPoint point(*this);
35 this->x++;
36 this->y++;
37 this->z++;
38 return point;//返回自增前的对象
39 }
40
41 TDPoint operator++(TDPoint& point)
42 {
43 ++point.x;
44 ++point.y;
45 ++point.z;
46 return point;//返回自增后的对象
47 }
48
49 TDPoint operator++(TDPoint& point,int)
50 {
51     TDPoint point1(point);
52     point.x++;
53     point.y++;
54     point.z++;
55 return point1;//返回自增前的对象
56 }
57
58 void TDPoint::showPoint()
59 {
60     std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl;
61 }
62
63 int main()
64 {
65     TDPoint point(1,1,1);
66     point.operator++();//或++point
67     point.showPoint();//前置++运算结果
68
69     point=point.operator++(0);//或point=point++
70     point.showPoint();//后置++运算结果
71
72 operator++(point);//或++point;
73     point.showPoint();//前置++运算结果
74
75     point=operator++(point,0);//或point=point++;
76     point.showPoint();//后置++运算结果
77
78 return0;
79 }


结果:



从示例代码里可以清楚的看出,后置运算符重载函数比前置运算符重载函数多了一个int类型的参数,这个参数只是为了区别前置和后置运算符,此外没有任何作用。所以在调用后置运算符重载函数时,int类型的实参可以取任意值。

  2.在C++中,操作符"<<"和">>"被定义为左位移运算符和右位移运算符。由于在iostream头文件中对它们进行了重载,使得它们可以用基本数据的输出和输入。

#include "stdafx.h"
#include <iostream>

int main()
{
int a=10;
std::cout<<"a="<<a<<std::endl;//运算符"<<"重载后用于输出
a=a>>2;//右移运算符
std::cout<<"右移2位:a="<<a<<std::endl;

std::cout<<"请输入一个整数a:";
std::cin>>a;//运算符">>"重载后用于输入
a=a<<2;//左移运算符
std::cout<<"左移2位:a="<<a<<std::endl;

return0;
}


结果:



插入运算符"<<"是双目运算符,左操作数为输出流类ostream的对象,右操作数为系统预定义的基本类型数据。头文件iostrem对其重载的函数原型为ostream& operator<<(ostream& ,类型名);类型名就是指基本类型数据。但如果要输出用户自定义的类型数据的话,就需要重载操作符"<<",因为该操作符的左操作数一定为ostream类的对象,所以插入运算符"<<"只能是类的友元函数或普通函数,不能是其他类的成员函数。一般定义格式:

  ostream& operator<<(ostream& ,自定义类名&);

提取运算符">>"也是如此,左操作数为istream类的对象,右操作数为基本类型数据。头文件iostrem对其重载的函数原型为istream& operator>>(istream& ,类型名);提取运算符也不能作为其他类的成员函数,可以是友元函数或普通函数。它的一般定义格式为:

  istream& operator>>(istream& ,自定义类名&);

我还是用上一节用的Complex类(复数类)来举例:

#include "stdafx.h"
#include <iostream>

class Complex //复数类
{
private://私有
double real;//实数
double imag;//虚数
public:
Complex(double real=0,double imag=0)
{
this->real=real;
this->imag=imag;
}
friend std::ostream&operator<<(std::ostream& o,Complex& com);//友元函数重载提取运算符"<<"
friend std::istream&operator>>(std::istream& i,Complex& com);//友元函数重载插入运算符">>"
};

std::ostream&operator<<(std::ostream& o,Complex& com)
{
std::cout<<"输入的复数:";
o<<com.real;
if(com.imag>0)
o<<"+";
if(com.imag!=0)
o<<com.imag<<"i"<<std::endl;
return o;
}

std::istream&operator>>(std::istream& i,Complex& com)
{
std::cout<<"请输入一个复数:"<<std::endl;
std::cout<<"real(实数):";
i>>com.real;
std::cout<<"imag(虚数):";
i>>com.imag;
return i;
}

int main()
{

Complex com;
std::cin>>com;
std::cout<<com;

return0;
}


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: