程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ 操作符重載

C++ 操作符重載

編輯: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 } 復制代碼  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 } 復制代碼  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 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved