C++的運算子多載--思考1

來源:互聯網
上載者:User

標籤:

   學習C++ 我們最深刻的體會就是C++不像那個C同學,是那樣的單純,我們表面上一看就透,是啥就是傻。C++中所有的東西都是為物件導向而設計的,所以不免有些黑盒之類到的思想,就是很多表面的東西,在內部是經過複雜的封裝的,所以這個C++同學已經別社會汙染,變得城府很深了,我們要想瞭解這位C++同學,就必須從他的外表之下去偷窺他內心那單純的性格,只有這樣才能看到它的內心世界!

  今天我想討論一下運算子多載,其實說白了就是一個函數重載,只是這個函數的名字有點怪而已。看這一點就可以體現出C++ 同學的城府很深吧,在C語言中如果重名了,就不認識了,但是對於C++ 同學他可是可以看透重名的不同喲。

   今天只思考一下,既然運算子多載是函數重載,那對於C++來說到底用  成員函數,普通函數,還是友員函數進行重載呢


今天拿複數的四則運算作為一個例子:

1.運算子多載作為普通函數:

/**********************************************************************    * *   Copyright (c)2015,WK Studios  * *   Filename:  A.cpp* *   Compiler: GCC  vc 6.0   * *   Author:WK    * *   Time: 2015 4 5  * **********************************************************************/#include<iostream>using std::cout;using std::cin;class Complex{public:double real;double imag;private:};//普通函數(沒有this指標只能訪問類的公有部分)Complex operator+(Complex &a,Complex &b);Complex operator-(Complex &a,Complex &b);Complex operator*(Complex &a,Complex &b);Complex operator/(Complex &a,Complex &b);int main(){   Complex com1(1.1,2.3),com2(2.2,4.3);   Complex  total;   total=com1/com2;//隱式調用   cout<<total.real<<"   "<<total.imag<<"\n";   total=operator/(com1,com2);//與上面的等價,這是顯式調用   cout<<total.real<<"   "<<total.imag<<"\n";return 0;}Complex operator+(Complex &a,Complex &b){return Complex(a.real+b.real,a.imag+b.imag);}Complex operator-(Complex &a,Complex &b){return Complex(a.real-b.real,a.imag-b.imag);}Complex operator*(Complex &a,Complex &b){return Complex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real);}Complex operator/(Complex &a,Complex &b){return Complex((a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*a.imag-a.real*b.imag)/(b.real*b.real+b.imag*b.imag));}

對於用重載運算子作為普通函數是很少用的,這樣只能訪問類的公有部分,當資料與方法在公有部分時候可以使用,但是C++強調安全,封裝,這種情況應該很少!

2.運算子多載作為成員函數:

/**********************************************************************    * *   Copyright (c)2015,WK Studios  * *   Filename:  A.h* *   Compiler: GCC  vc 6.0   * *   Author:WK    * *   Time: 2015 4 5  * **********************************************************************/#include<iostream>using std::cout;using std::cin;class Complex{public://建構函式Complex(double r=0.0,double i=0.0):real(r),imag(i){}//複數加//成員函數Complex operator+(Complex &b);//複數減//成員函數Complex operator-(Complex &b);//複數乘//成員函數Complex operator*(Complex &b);//複數除//成員函數Complex operator/(Complex &b);    void print(){ cout<<real<<"   "<<imag<<"\n";}private:double real;double imag;};int main(){   Complex com1(1.1,2.3),com2(2.2,4.3);   Complex  total;   total=com1+com2;//隱式調用   total.print();   total=com1.operator+(com2);////與上面的等價,這是顯式調用   total.print();  return 0;}Complex Complex ::operator+(Complex &b){return Complex(real+b.real,imag+b.imag);}Complex Complex:: operator-(Complex &b){return Complex(real-b.real,imag-b.imag);}Complex Complex:: operator*(Complex &b){return Complex(real*b.real-imag*b.imag,real*b.imag+imag*b.real);}Complex Complex:: operator/(Complex &b){return Complex((real*b.real+imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*imag-real*b.imag)/(b.real*b.real+b.imag*b.imag));}</span>

成員函數對於很多運算子都要重載為這種形式,使用較多

3.運算子多載作為友員函數:

友員函數只屬於類,不屬於任何對象所以他不需要對象的this指標來確定自己的資料,但是沒有指標this就不能使用,在這裡這個有點像普通函數,只是只是友員函數時類的朋友,可以訪問類的私密資料,單是具體什麼時候使用友員函數最好,這是因時而宜!

/**********************************************************************    * *   Copyright (c)2015,WK Studios  * *   Filename:  A.h* *   Compiler: GCC  vc 6.0   * *   Author:WK    * *   Time: 2015 4 5  * **********************************************************************/#include<iostream>using std::cout;using std::cin;class Complex{public://建構函式Complex(double r=0.0,double i=0.0):real(r),imag(i){}//友員函數(無this指標)friend Complex operator+(Complex &a,Complex &b);//友員函數(無this指標)friend Complex operator-(Complex &a,Complex &b);//友員函數(無this指標)    friend Complex operator/(Complex &a,Complex &b);//友員函數(無this指標)     friend Complex operator*(Complex &a,Complex &b);    void print(){ cout<<real<<"   "<<imag<<"\n";}private:double real;double imag;};int main(){   Complex com1(1.1,2.3),com2(2.2,4.3);   Complex  total;   total=com1+com2;//隱式調用   total.print();   total=operator+(com1,com2);////與上面的等價,這是顯式調用   total.print();  return 0;}Complex operator+(Complex &a,Complex &b){return Complex(a.real+b.real,a.imag+b.imag);}Complex operator-(Complex &a,Complex &b){return Complex(a.real-b.real,a.imag-b.imag);}Complex operator*(Complex &a,Complex &b){return Complex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real);}Complex operator/(Complex &a,Complex &b){return Complex((a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag),(b.real*a.imag-a.real*b.imag)/(b.real*b.real+b.imag*b.imag));}

最後說一下運算子多載各種形式函數的適用時候:




 

C++的運算子多載--思考1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.