操作符重载

一.不能被重载的操作符

?: C++中无定义一个三目运算符的语法

.      安全性

*     安全性

:: 该操作符左边是一个类型名,不是一个表达式

二.<返回值类型> operator <运算符> (<形式参数表>)

1.重载一个一元操作符,其函数不需要任何参数

2.操作符重载,不改变优先级和结合性

3.重载不改变操作符的语法

4.一元重载仍是一元,二元重载仍是二元

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Pair
 5 {
 6 public:
 7     Pair(int x, int y)
 8     {
 9         p1 = x; p2 = y;
10     }
11     
12     bool operator==(const Pair &p)
13     {
14         return p1 == p.p1 && p2 == p.p2;
15     }
16 
17 private:
18     int p1, p2;
19 };
20 
21 
22 int main()
23 {
24     Pair s1(1, 2), s2(2, 3);
25     cout << boolalpha << (s1 == s2) << endl;//false
26     return 0;
27 }
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Complex
 5 {
 6 public:
 7     Complex()
 8     {
 9         real = 0;
10         imag = 0;
11     }
12     
13     Complex(double r)
14     {
15         real = r;
16         imag = 0;
17     }
18 
19     Complex(double r, double i)
20     {
21         real = r;
22         imag = i;
23     }
24 
25     Complex operator+(const Complex &c) const
26     {
27         Complex s;
28         s.real = real + c.real;
29         s.imag = imag + c.imag;
30         return s;
31     }
32 
33     Complex operator-(const Complex &c)
34     {
35         Complex s;
36         s.real = real - c.real;
37         s.imag = imag - c.imag;
38         return s;
39     }
40 
41     double real, imag;
42 };
43 
44 
45 int main()
46 {
47     Complex s1(1, 2), s2(2, 3);
48     Complex s = s1 + s2;
49     cout << s.real << " " << s.imag << endl;//3 5
50     return 0;
51 }

三.用顶层函数进行操作符重载

1.以顶层函数进行操作符重载必须在它的参数表中包含一个类对象,new, new[], delete, delete[]除外。没有类对象,如:void operator%(float f1, float f2)//error,x % y不能区别%是内建操作符还是用户定义的。

2.对于成员函数重载二元操作符,只需要一个参数。对顶函数需要2个。

[], =, ()(函数调用操作符), ->必须以类成员函数重载

void operator[](int i, Point &s);//error

因为9[x], 6.2 = x是不允许的

3.

1 Complex operator+(const Complex &c) const{......}
2 
3 Complex a, b(4.3, -8.2);
4 a = b + 54.3;//ok,转型构造
5 a = 54.3 + b; //error,54.3.operator+(b)
1 Complex operator+(const Complex &c, const Complex &b) {......}
2 
3 Complex a, b(4.3, -8.2);
4 a = b + 54.3;//ok
5 a = 54.3 + b; //ok

4.顶层:非对象操作数可出现在操作符左边

   类成员函数:第一个操作数必须是类对象

5.可把顶层函数设成类的friend

四.friend

类的friend函数可以访问私有成员和保护成员,但不是类的成员函数

建议仅在重载操作符时使用friend

1.friend函数可以是另一个类的成员函数

 1 class Date;
 2 
 3 class Time
 4 {
 5 public:
 6     void display(){}
 7 };
 8 
 9 class Date
10 {
11     friend void Time::display();
12 };

2.友元类

1 class A{};
2 
3 class B
4 {
5     friend A;
6 };
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class CCar
 5 {
 6 private:
 7     int price;
 8     friend class CDriver;
 9 };
10 
11 class CDriver
12 {
13 public:
14     CCar myCar;
15     void ModifyCar()
16     {
17         myCar.price = 200;
18     }
19 };
20 
21 int main()
22 {
23     return 0;
24 }

A是B的友元类,类B所有的成员可以被A访问

friend是单向的,friend不能传递,不能继承

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class  CCar;
 5 
 6 class CDriver
 7 {
 8 public:
 9     void ModifyCar(CCar *pCar);
10 };
11 
12 class CCar
13 {
14 public:
15     CCar()
16     {
17         price = 100;
18     }
19     CCar(int i)
20     {
21         price = i;
22     }
23     friend int MostExpensiveCar(CCar cars[], int total);
24     friend void CDriver::ModifyCar(CCar *pCar);
25 private:
26     int price;
27 };
28 
29 void CDriver::ModifyCar(CCar *pCar)
30 {
31     pCar->price += 100;
32 }
33 
34 int MostExpensiveCar(CCar cars[], int total)
35 {
36     int tmpMax = -1;
37     for (int i = 0; i < total; ++i)
38     {
39         if (cars[i].price > tmpMax)
40         {
41             tmpMax = cars[i].price;
42         }
43     }
44     return tmpMax;
45 }
46 
47 int main()
48 {
49     CCar cars[100];
50     CDriver d;
51     
52     d.ModifyCar(&cars[99]);
53 
54     cout << MostExpensiveCar(cars, 100) << endl;//200
55 
56     return 0;
57 }
原文地址:https://www.cnblogs.com/wanderingzj/p/5295930.html