C++ 操作符重载

1.什么是操作符重载

  可以使用分词将操作符重载理解为:操作符+重载。

  C++中的操作符很多,如+,-,*,等等。

  C++中的重载也是C++中面向对象多态的体现。

  简单说操作符重载:

    C++中有:int a=2+3;  那么a=5

    操作符重载可以实现对自定义类型的操作:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Point{
 5 public:
 6     int x;
 7     int y;
 8     Point(int _x,int _y):x(_x),y(_y){
 9     }
10 
11     Point operator+(Point &p){
12         int t1=this->x+p.x;
13         int t2=this->y+p.y;
14         Point t(t1,t2);
15         return t;
16     }
17 };
18 
19 int main()
20 {
21     Point p1(1,3);
22     Point p2(2,6);
23     Point p3 = p1+p2;
24     cout<<"p3:("<<p3.x<<","<<p3.y<<")";     ///执行输出:p3:(3,9)
25     return 0;
26 }
View Code

 2.操作符重载的方式

  操作符重载的实现方式有两种,即通过“友元函数”或者“类成员函数”。如下面代码显示了这两种操作符重载:

 1 class Point{
 2 public:
 3     int x;
 4     int y;
 5     Point(int _x,int _y);
 6 
 7     Point operator+(Point &p);  ///类成员函数,类成员函数可以使用this指针获取自身对象
 8 
 9     friend int operator*(Point &p1,Point &p2);  ///友元函数
10 };

  可以看出,一个重载“+”,一个重载“*”,都是双目运算符,但是类成员函数只有一个参数。这是因为类成员函数可以使用this指针获取自身的对象,而友元函数则不行。

  所以类成员实现操作符重载需要的形式参数比原来少一个,这样使用类成员函数实现一元操作符就不需要参数了。

3.操作符重载例子

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Point{
 5 public:
 6     int x;
 7     int y;
 8     Point(int _x,int _y):x(_x),y(_y){
 9     }
10 
11     Point operator+(Point &p){  ///实现坐标向量加
12         int t1=this->x+p.x;
13         int t2=this->y+p.y;
14         Point t(t1,t2);
15         return t;
16     }
17 
18     friend int operator*(Point &p1,Point &p2);  ///实现内积
19 };
20 
21 int operator*(Point &p1,Point &p2)
22 {
23     return (p1.x*p2.x)+(p1.y*p2.y);
24 }
25 
26 int main()
27 {
28     Point p1(1,3);
29     Point p2(2,6);
30     cout<<p1*p2<<endl;  ///输出内积:20
31 
32     Point p3 = p1+p2;
33     cout<<"p3:("<<p3.x<<","<<p3.y<<")"<<endl;   ///输出坐标和:(3,9)
34     return 0;
35 }
View Code

 4.重载操作符注意事项

(1)重载操作符必须有一个类类型的参数。也就是说不能 int  operator+(int,int);

(2)操作符的优先级和结核性是固定的。

(3)不在具有短路求值特性。如&&、||等,两个数都会进行求值,而且顺序不定。

(4)不要重载具有内置含义的操作符。重载操作符主要是弥补普通操作符对类类型数据的作用。像赋值操作符、取地址操作符、逗号操作符等对类类型操作数都有默认的含义。

(5)只能对已有的C++运算符进行重载,不允许用户自己定义新的运算符。

(6)绝大部分的运算符可重载,除了成员访问运算符.,作用域运算符::,长度运算符sizeof以及条件运算符?:

(7)运算符重载后不能改变运算符的操作对象(操作数)的个数。

5.特例操作符"++"、"--"的重载

  自增、自减操作符有前后之分,通过一个无意义的整数参数来区分。无参数的是前置运算符,带参数的是后置运算符。

  如:

    int operator++();  //前置

    int operator++(int x);  //后置

  自增操作符实例

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Array{
 5 public:
 6     Array(int _num){    ///初始化一个数组,大小为_num
 7         this->a=new int[_num];
 8         this->num=_num;
 9         this->pos=0;
10         for(int i=0;i<num;i=i+1)
11             *((this->a)+i)=i;
12     }
13 
14     ~Array(){
15         delete [] a;
16     }
17 
18     int operator++(){  ///++前置
19         if(pos == num-1){
20             cout<<"Fuck"<<endl;
21             return 0;
22         }
23         return *((this->a)+(++pos));
24     }
25 
26     int operator++(int x){   ///++后置
27         if(pos == num-1){
28             cout<<"Fuck"<<endl;
29             return 0;
30         }
31         return *((this->a)+(pos++));
32     }
33 
34     int *a;
35     int pos;
36     int num;
37 };
38 
39 int main()
40 {
41     Array array(100);
42     cout<<"pos="<<array.pos<<endl;
43     cout<<"pre_position:"<<(++array)<<endl;
44     cout<<"pos="<<array.pos<<endl;
45     cout<<"********************************************************"<<endl;
46 
47     cout<<"pos="<<array.pos<<endl;
48     cout<<"post_position:"<<(array++)<<endl;
49     cout<<"pos="<<array.pos<<endl;
50     cout<<"********************************************************"<<endl;
51     return 0;
52 }
View Code

执行结果:

原文地址:https://www.cnblogs.com/xudong-bupt/p/3557525.html