運算符重載(C++)
一、運算符重載機制:
一元運算符: @obj => operator @(obj)
二元運算符: obj@obj2 => operator @(obj,obj2)
注意:前置++、--與一元運算符處理方式相同,而後置++、--這樣處理:obj++ => operator ++(obj,0)
二、除了.、.*、::、?:、sizeof這5個運算符之外,其他一概都可以重載。
三、普通運算符重載:
1.重載為類的友元函數:
1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double image;
9 public:
10 Complex(double real=0,double image=0){ this->real=real,this->image=image; }
11 void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
12 friend Complex operator + (Complex A,Complex B){ return Complex(A.real+B.real,A.image+B.image); }//加法
13 friend Complex operator - (Complex A,Complex B);//減號
14 friend Complex operator - (Complex A);//負號
15 friend Complex operator ++ (Complex& A);//前置++
16 friend Complex operator ++ (Complex& A,int);//後置++
17 };
18 Complex operator - (Complex A,Complex B) { return Complex(A.real-B.real,A.image-B.image); }
19 Complex operator - (Complex A) { return Complex(-A.real,-A.image); }
20 Complex operator ++(Complex& A) { return Complex(++A.real,A.image); }
21 Complex operator ++(Complex& A,int) { return Complex(A.real++,A.image); }
22
23 int main()
24 {
25 Complex A(100.0,200.0),B(-10.0,20.0),C;
26 cout<<"A=";A.display();
27 cout<<"B=";B.display();
28 C=A+B;
29 cout<<"C=A+B=";C.display();
30 C=A-B;
31 cout<<"C=A-B=";C.display();
32 C=-A+B;
33 cout<<"C=-A+B=";C.display();
34 C=A++;
35 cout<<"C=A++,C=";C.display();
36 C=++A;
37 cout<<"C=++A,C=";C.display();
38 C=A+5;
39 cout<<"C=A+5=";C.display();
40 return 0;
41 }
2.重載為類的成員函數:
1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double image;
9 public:
10 Complex(double real=0,double image=0){ this->real=real,this->image=image; }
11 void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
12 Complex operator + (Complex B);//加法
13 Complex operator - (Complex B);//減號
14 Complex operator - ();//負號
15 Complex operator ++ ();//前置++
16 Complex operator ++ (int);//後置++
17 };
18 Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
19 Complex Complex::operator - (Complex B) { return Complex(real-B.real,image-B.image); }
20 Complex Complex::operator - () { return Complex(-real,-image); }
21 Complex Complex::operator ++() { return Complex(++real,image); }
22 Complex Complex::operator ++(int) { return Complex(real++,image); }
23
24 int main()
25 {
26 Complex A(100.0,200.0),B(-10.0,20.0),C;
27 cout<<"A=";A.display();
28 cout<<"B=";B.display();
29 C=A+B;
30 cout<<"C=A+B=";C.display();
31 C=A-B;
32 cout<<"C=A-B=";C.display();
33 C=-A+B;
34 cout<<"C=-A+B=";C.display();
35 C=A++;
36 cout<<"C=A++,C=";C.display();
37 C=++A;
38 cout<<"C=++A,C=";C.display();
39 C=A+5;
40 cout<<"C=A+5=";C.display();
41 return 0;
42 }
3.兩種重載方式的比較:
1)一般情況下,單目運算符最好重載為類的成員函數;雙目運算符則最好重載為類的友元函數。
2)一些雙目運算符不能重載為類的友元函數:=、()、[]、->。
3)若一個運算符的操作需要改變對象的狀態,選擇重載為成員函數較好。
4)若運算符所需的操作數(尤其第一個操作數)希望有隱式類型轉換,則只能選用友元函數。
5)當運算符函數是一個成員函數時,最左邊的操作數必須是運算符類的一個類對象或其引用。若左邊的操作數必須是一個不同類的對象,或者是
一個基本數據類型的對象,該運算符函數必須作為一個友元函數來實現。
6)當需要重載運算符的運算具有可交換性時,選擇重載為友元函數。
四、典型運算符重載:
1.重載=進行復數類數據賦值:
1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double image;
9 public:
10 Complex(double real=0,double image=0) { this->real=real,this->image=image; }
11 void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
12 Complex operator + (Complex B);
13 Complex operator = (Complex B);
14 };
15 Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
16 Complex Complex::operator =(Complex B)
17 {
18 real=B.real,image=B.image;
19 cout<<"operator = calling..."<<endl;
20 return *this;
21 }
22 int main()
23 {
24 Complex A(100.0,200.0),B(-10.0,20.0),C;
25 cout<<"A=",A.display();
26 cout<<"B=",B.display();
27 C=A+B;
28 cout<<"C=A+B=",C.display();
29 C=A;
30 cout<<"C=A=",C.display();
31 }
2.->:
1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double image;
9 public:
10 Complex(double real=0,double image=0) { this->real=real,this->image=image; }
11 void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
12 };
13 class PComplex
14 {
15 private:
16 Complex *PC;
17 public:
18 PComplex(Complex *PC=NULL) { this->PC=PC; }
19 Complex * operator ->()
20 {
21 static Complex NullComplex(0.0);
22 if(PC==NULL) return &NullComplex;
23 return PC;
24 }
25 };
26 int main()
27 {
28 PComplex P1;
29 P1->display();
30 Complex C1(100,200);
31 P1=&C1;
32 P1->display();
33 return 0;
34 }
3.[]:
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 class String
6 {
7 private:
8 char *str;
9 int len;
10 public:
11 void showstr() { cout<<"string:"<<str<<",length:"<<len<<endl; }
12 String(const char *p=NULL)
13 {
14 if(p)
15 {
16 len=strlen(p);
17 str=new char[len+1];
18 strcpy(str,p);
19 }
20 else
21 {
22 len=0;
23 str=NULL;
24 }
25 }
26 ~String()
27 {
28 if(str!=NULL) delete []str;
29 }
30 char &operator[](int n) { return *(str+n); }
31 const char &operator[](int n)const { return *(str+n); }
32 };
33
34 int main()
35 {
36 String S1("0123456789abcdef");
37 S1.showstr();
38 S1[10]='A';
39 cout<<"S1[10]=A"<<endl;
40 S1.showstr();
41 const String S2("ABCDEFGHIJKLMN");
42 cout<<"S2[10]="<<S2[10]<<endl;
43 return 0;