操作符重载
操作符重载:
在自定义的数据类型的数据间的加减乘除操作,默认情况下编译器是不识别的,需要进行操作重载,格式如operator+(param);
但是并不是所有的运算符都是可以重载或有必要重载的!
重载规则:
实例代码:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdlib> 4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) { 10 this->a = a; 11 this->b = b; 12 } 13 14 void print_complex() { 15 cout << "(" << this->a << "," << this->b << "i)" << endl; 16 } 17 18 Complex complex_add(Complex &another) { 19 Complex temp(this->a + another.a, this->b + another.b); 20 return temp; 21 } 22 Complex operator+(Complex &another) { 23 Complex temp(this->a + another.a, this->b + another.b); 24 return temp; 25 } 26 27 friend Complex complex_add(Complex &c1, Complex &c2); 28 //friend Complex operator+(Complex &c1, Complex &c2); 29 private: 30 int a;//实部 31 int b;//虚部 32 }; 33 34 //对象加法的全局函数 35 Complex complex_add(Complex &c1, Complex &c2) { 36 Complex temp(c1.a + c2.a, c1.b + c2.b); 37 return temp; 38 } 39 40 #if 0 41 //操作符重载,全局 42 Complex operator+(Complex &c1, Complex &c2) { 43 Complex temp(c1.a + c2.a, c1.b + c2.b); 44 return temp; 45 } 46 #endif 47 48 int main(void) { 49 Complex c1(1, 2); 50 Complex c2(2, 4); 51 52 c1.print_complex(); 53 c2.print_complex(); 54 55 //Complex c3 = complex_add(c1, c2); 56 //Complex c3 = c1.complex_add(c2); 57 58 //默认情况下,'+'对于自定义的数据类型编译器是不识别的 59 //需要操作符重载 60 Complex c3 = c1 + c2;//c1+c2会同时匹配全局的operator+(c1,c2)和局部的c1.operator+(c2) 61 c3.print_complex(); 62 Complex c4 = c1 + c2 + c3;//可以满足连加 63 c4.print_complex(); 64 system("pause"); 65 return 0; 66 }
双目运算符'+='的重载:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdlib> 4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) { 10 this->a = a; 11 this->b = b; 12 } 13 14 void print_complex() { 15 cout << "(" << this->a << "," << this->b << "i)" << endl; 16 } 17 18 Complex & operator+=(Complex &another) { 19 this->a += another.a; 20 this->b += another.b; 21 return *this; 22 } 23 24 //friend Complex & operator+=(Complex &c1, Complex &c2); 25 26 private: 27 int a;//实部 28 int b;//虚部 29 }; 30 31 32 #if 0 33 //操作符重载,全局 34 Complex & operator+=(Complex &c1, Complex &c2) { 35 c1.a += c2.a; 36 c1.b += c2.b; 37 return c1; 38 } 39 #endif 40 41 int main(void) { 42 Complex c1(1, 2); 43 Complex c2(2, 4); 44 45 c1.print_complex(); 46 c2.print_complex(); 47 48 (c1 += c2) += c2; 49 c1.print_complex(); 50 system("pause"); 51 return 0; 52 }
单目运算符'++'的重载:

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdlib> 4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) { 10 this->a = a; 11 this->b = b; 12 } 13 14 void print_complex() { 15 cout << "(" << this->a << "," << this->b << "i)" << endl; 16 } 17 //前++ 18 Complex & operator++() { 19 this->a++; 20 this->b++; 21 return *this; 22 } 23 //后++ 24 const Complex operator++(int) {//int称为亚元 25 Complex temp(this->a, this->b); 26 this->a++;//***让当前对象的属性值++ 27 this->b++; 28 return temp;//返回临时变量的值还是当前对象++之前的值,从而实现了先用后加的效果 29 } 30 //前++返回引用 31 //friend Complex & operator++(Complex &c); 32 33 //后++返回值不可以修改所以用了const修饰,占位符int只是为了区分后++ 34 //friend const Complex operator++(Complex &c, int); 35 36 private: 37 int a;//实部 38 int b;//虚部 39 }; 40 41 42 #if 0 43 //操作符前++重载,全局 44 Complex & operator++(Complex &c) { 45 c.a++; 46 c.b++; 47 return c; 48 } 49 #endif 50 51 #if 0 52 //操作符后++重载,全局 53 const Complex operator++(Complex &c,int) { 54 Complex temp(c.a, c.b); 55 c.a++; 56 c.b++; 57 return temp;//返回的是值,用中间变量的方式实现后++的先用后加的效果 58 } 59 #endif 60 61 int main(void) { 62 Complex c1(1, 2); 63 Complex c2(2, 4); 64 65 c1.print_complex(); 66 c2.print_complex(); 67 68 ++++c1; 69 c1.print_complex(); 70 71 c2++; 72 c2.print_complex(); 73 system("pause"); 74 return 0; 75 }
左移右移操作符重载:
左移右移操作符的重载都必须写在类的外部,全局的,否则写在类内部会出现调用时顺序变反,如:c1<<cout。

1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdlib> 4 5 using namespace std; 6 7 class Complex { 8 public: 9 Complex(int a, int b) { 10 this->a = a; 11 this->b = b; 12 } 13 14 void print_complex() { 15 cout << "(" << this->a << "," << this->b << "i)" << endl; 16 } 17 18 friend ostream & operator<<(ostream &os,Complex &c); 19 20 friend istream & operator>>(istream &is,Complex &c); 21 22 private: 23 int a;//实部 24 int b;//虚部 25 }; 26 27 28 #if 1 29 30 ostream & operator<<(ostream &os, Complex &c) { 31 os << "(" << c.a << "," << c.b << "i)" << endl; 32 return os; 33 } 34 istream & operator>>(istream &is, Complex &c) { 35 cout << "a:"; 36 is >> c.a; 37 cout << "b:"; 38 is >> c.b; 39 return is; 40 } 41 #endif 42 43 int main(void) { 44 Complex c1(1, 2); 45 46 cin >> c1; 47 cout << c1; 48 system("pause"); 49 return 0; 50 }
’=‘操作符的重载,首先判断是不是自身赋值,然后将自身额外开辟的空间回收,最后再执行深拷贝。
仿函数(functor)是把类对象像函数一样使用,其实就是一个类实现了operator(),使得类对象有了函数行为。
’()‘操作符重载,一般用于仿函数(伪函数)的使用,譬如给一个对象排序就需要()重载的仿函数实现。
//()重载,也可以传多个参数 int operator()(int value1, int value2) { return value1*value2; }
new和delete操作符也可以重载,有类A,其私有成员为 int a;
new的重载使用C语言的malloc()函数,但这个重载仍然会出发对象的构造函数;
delete的重载使用C语言的free()函数,但这个重载仍然也会触发对象的析构函数。