C++运算符重载

  本文主要整理自C++之运算符重载C++运算符重载总结

什么是运算符重载

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

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;
View Code

  在这个程序里"+"既完成两个整形数的加法运算,又完成了双精度型的加法运算。为什么同一个运算符"+"可以用于完成不同类型的数据的加法运算?这是因为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 }
View Code

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

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

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

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

  结果:

  

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

类的运算符重载

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

运算符重载函数作为类的友元函数的形式

class 类名
{
    friend 返回类型 operator运算符(形参表);
}

类外定义格式:
返回类型 operator运算符(参数表)
{
    函数体
}

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

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

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

  结果:

  

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

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

  结果:

  

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

运算符重载函数作为类的成员函数的形式

class 类名
{
    返回类型 operator 运算符(形参表);
}

类外定义格式:
返回类型 类名:: operator 运算符(形参表)
{
    函数体;
}

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

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

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

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

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

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

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

  在运算符重载运用时应该注意以下几个问题:

  (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     // total = 5 + com1; // 编译不通过
25     return 0;
26 }
View Code

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

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

  完整程序为:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Complex //复数类
 6 {
 7 private://私有
 8     double real;//实数
 9     double imag;//虚数
10 public:
11     Complex(double real = 0, double imag = 0)
12     {
13         this->real = real;
14         this->imag = imag;
15     }
16     friend Complex operator+(Complex& com, int x);
17     friend Complex operator+(int x, Complex& com);
18 };
19 
20 Complex operator+(Complex& com, int x)
21 {
22     return Complex(com.real + x, com.imag);
23 }
24 
25 Complex operator+(int x, Complex& com)
26 {
27     return Complex(com.real + x, com.imag);
28 }
29 
30 int main()
31 {
32     Complex com1(5, 10), total;
33     total = com1 + 5;
34     total = 5 + total; // 编译通过
35 
36     return 0;
37 }
View Code

  博文C++之运算符重载还作了一次完整的程序测试,结果如下: 

  

运算符重载中的“前置”及“后置”问题及操作符“<<”、“>>”重载

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

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

  见程序:

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

  结果:

  

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

操作符"<<"和">>"重载

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

 1 #include <iostream>
 2 
 3 int main()
 4 {
 5     int a = 10;
 6     std::cout << "a=" << a << std::endl;//运算符"<<"重载后用于输出
 7     a = a >> 2;//右移运算符
 8     std::cout << "右移2位:a=" << a << std::endl;
 9 
10     std::cout << "请输入一个整数a:";
11     std::cin >> a;//运算符">>"重载后用于输入
12     a = a << 2;//左移运算符
13     std::cout << "左移2位:a=" << a << std::endl;
14 
15     return 0;
16 }
View Code 

  结果:

  

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

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

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

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

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

 1 #include <iostream>
 2 
 3 class Complex //复数类
 4 {
 5 private://私有
 6     double real;//实数
 7     double imag;//虚数
 8 public:
 9     Complex(double real = 0, double imag = 0)
10     {
11         this->real = real;
12         this->imag = imag;
13     }
14     friend std::ostream&operator<<(std::ostream& o, Complex& com);//友元函数重载提取运算符"<<"
15     friend std::istream&operator>>(std::istream& i, Complex& com);//友元函数重载插入运算符">>"
16 };
17 
18 std::ostream&operator<<(std::ostream& o, Complex& com)
19 {
20     std::cout << "输入的复数:";
21     o << com.real;
22     if (com.imag > 0)
23         o << "+";
24     if (com.imag != 0)
25         o << com.imag << "i" << std::endl;
26     return o;
27 }
28 
29 std::istream&operator>>(std::istream& i, Complex& com)
30 {
31     std::cout << "请输入一个复数:" << std::endl;
32     std::cout << "real(实数):";
33     i >> com.real;
34     std::cout << "imag(虚数):";
35     i >> com.imag;
36     return i;
37 }
38 
39 int main()
40 {
41     Complex com;
42     std::cin >> com;
43     std::cout << com;
44 
45     return 0;
46 }
View Code

  结果:

  

运算符重载参考范例

  博文C++运算符重载总结提供了一个关于常用的一些运算符重载的范例:

一般运算符重载

 1 class A
 2 {
 3 public:
 4     A(int d) :data(d){}
 5     A operator+(A&);//成员函数
 6     A operator-(A&);
 7     A operator*(A&);
 8     A operator/(A&);
 9     A operator%(A&);
10     friend A operator+(A&, A&);//友元函数
11     friend A operator-(A&, A&);
12     friend A operator*(A&, A&);
13     friend A operator/(A&, A&);
14     friend A operator%(A&, A&);
15 private:
16     int data;
17 };
18 
19 //成员函数的形式
20 A A::operator+(A &a)
21 {
22     return A(data + a.data);
23 }
24 A A::operator-(A &a)
25 {
26     return A(data - a.data);
27 }
28 
29 A A::operator*(A &a)
30 {
31     return A(data*a.data);
32 }
33 A A::operator/(A &a)
34 {
35     return A(data / a.data);
36 }
37 A A::operator%(A &a)
38 {
39     return A(data%a.data);
40 }
41 
42 //友元函数的形式
43 A operator+(A &a1, A &a2)
44 {
45     return A(a1.data + a2.data);
46 }
47 A operator-(A &a1, A &a2)
48 {
49     return A(a1.data - a2.data);
50 }
51 A operator*(A &a1, A &a2)
52 {
53     return A(a1.data*a2.data);
54 }
55 A operator/(A &a1, A &a2)
56 {
57     return A(a1.data / a2.data);
58 }
59 A operator%(A &a1, A &a2)
60 {
61     return A(a1.data%a2.data);
62 }
View Code

关系运算符重载

1 bool operator == (const A&);
2 bool operator != (const A&);
3 bool operator < (const A&);
4 bool operator <= (const A&);
5 bool operator > (const A&);
6 bool operator >= (const A&);
View Code

逻辑运算符重载

1 bool operator || (const A&);
2 bool operator && (const A&);
3 bool operator ! ();
View Code

单目运算符重载

1 A& operator + ();
2 A& operator - ();
3 A* operator & ();
4 A& operator * ();
View Code

自增减运算符重载

1 A& operator ++ ();//前置++
2 A operator ++ (int);//后置++
3 A& operator --();//前置--
4 A operator -- (int);//后置--
View Code

位运算符重载

1 A operator | (const A&);
2 A operator & (const A&);
3 A operator ^ (const A&);
4 A operator << (int i);
5 A operator >> (int i);
6 A operator ~ ();
View Code

赋值运算符重载

  没有“=”。

 1 A& operator += (const A&);
 2 A& operator -= (const A&);
 3 A& operator *= (const A&);
 4 A& operator /= (const A&);
 5 A& operator %= (const A&);
 6 A& operator &= (const A&);
 7 A& operator |= (const A&);
 8 A& operator ^= (const A&);
 9 A& operator <<= (int i);
10 A& operator >>= (int i);
View Code

内存运算符重载

1 void *operator new(size_t size);
2 void *operator new(size_t size, int i);
3 void *operator new[](size_t size);
4 void operator delete(void*p);
5 void operator delete(void*p, int i, int j);
6 void operator delete[](void* p);
View Code

特殊运算符重载

 1 A& operator = (const A&);
 2 char operator [] (int i);//返回值不能作为左值
 3 const char* operator () ();
 4 T operator -> ();
 5 
 6 //类型转换符
 7 operator char* () const;
 8 operator int();
 9 operator const char() const;
10 operator short int() const;
11 operator long long() const;
12 
13 //还有很多...
View Code

  而这些只能以友元函数的形式重载:

1 friend inline ostream &operator << (ostream&, A&);//输出流
2 friend inline istream &operator >> (istream&, A&);//输入流

  更多内容稍后添加...

原文地址:https://www.cnblogs.com/xiehongfeng100/p/4040858.html