C++ 雙目運算子多載

來源:互聯網
上載者:User

標籤:color   this   pre   hat   +=   amp   c++   調用   operator   

  1 class Point  2 {  3     int x;  4     int y;  5 public:  6     //Point(void) { }  7     Point(int _x=0,int _y=0)  8     {  9         x = _x; 10         y = _y; 11     } 12      13     Point(Point& that) 14     { 15         x = that.x; 16         y = that.y; 17     } 18      19     Point operator + (Point& that) 20     { 21         Point t;//此行代碼會調用無參構造 22         t.x = x + that.x; 23         t.y = y + that.y; 24         return t; 25     } 26      27     Point operator += (Point& that) 28     { 29         x = x + that.x; 30         y = y + that.y; 31         return *this; 32     } 33      34     Point operator * (Point& that) 35     { 36         Point t; 37         t.x = x * that.x; 38         t.y = y * that.y; 39         return t; 40     } 41      42     Point operator ++ (int) 43     { 44         Point t(*    this);//此行代碼會調用無參構造 45         x++; 46         y++; 47         return t; 48     } 49  50     Point operator ++ () 51     { 52         x++; 53         y++; 54         return *this; 55     } 56      57     Point* operator -> (void) 58     { 59         return this; 60     } 61      62     friend ostream& operator << (ostream& os,Point& that) 63     { 64         return os << "x=" << that.x << "," << "y=" << that.y; 65     } 66      67     friend Point operator - (Point& dest,Point& that); 68     friend Point operator -= (Point& dest,Point& that); 69     friend Point operator / (Point& dest,Point& that); 70     friend Point operator -- (Point& that); 71     friend Point operator -- (Point& that,int); 72     friend istream& operator >> (istream& os,Point& that); 73 }; 74  75 Point operator - (Point& dest,Point& that) 76 { 77     Point t;//此行代碼會調用無參構造 78     t.x = dest.x - that.x; 79     t.y = dest.y - that.y; 80     return t; 81 } 82  83 Point operator -= (Point& dest,Point& that) 84 { 85     dest.x = dest.x - that.x; 86     dest.y = dest.y - that.y; 87     return dest; 88 } 89  90 Point operator / (Point& dest,Point& that) 91 { 92     Point t; 93     t.x = dest.x / that.x; 94     t.y = dest.y / that.y; 95     return t; 96 } 97      98 Point operator -- (Point& that,int) 99 {100     Point t(that);//此行代碼會調用無參構造101     that.x--;102     that.y--;103     return t;104 }105 106 Point operator -- (Point& that)107 {108     that.x--;109     that.y--;110     return that;111 }112 113 istream& operator >> (istream& is,Point& that)114 {115     return is >> that.x >> that.y;116 }

 

C++ 雙目運算子多載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.