C++运算符重载(二)——使用方法

来源:互联网 发布:淘宝扣48分封多久账号 编辑:程序博客网 时间:2024/05/22 12:59

原文:http://blog.csdn.net/linlinlinxi007/article/details/5283048

 

一、运算符重载的两种形式

  运算符重载一般采用如下两种形式:成员函数形式和友元函数形式。这两种形式都可访问类中的私有成员。

 

   1. 重载为类的成员函数

  这里先举一个关于给复数运算重载复数的四则运算符的例子。复数由实部和虚部构造,可以定义一个复数类,然后再在类中重载复数四则运算的运算符。先看以下源代码:

[cpp] view plaincopy
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class complex  
  5. {  
  6.     public:  
  7.         complex() { real=imag=0; }  
  8.         complex(double r, double i)  
  9.         {  
  10.             real = r, imag = i;  
  11.         }  
  12.         complex operator +(const complex &c);  
  13.         complex operator -(const complex &c);  
  14.         complex operator *(const complex &c);  
  15.         complex operator /(const complex &c);  
  16.         friend void print(const complex &c);  
  17.     private:  
  18.         double real, imag;  
  19. };   
  20.   
  21. inline complex complex::operator +(const complex &c)    //不申明为内联函数也可以,运算符函数是Complex类的成员函数,可以访问其保护成员变量  
  22. {  
  23.     return complex(real + c.real, imag + c.imag);  
  24. }   
  25.   
  26. inline complex complex::operator -(const complex &c)  
  27. {  
  28.     return complex(real - c.real, imag - c.imag);  
  29. }   
  30.   
  31. inline complex complex::operator *(const complex &c)  
  32. {  
  33.     return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);  
  34. }   
  35.   
  36. inline complex complex::operator /(const complex &c)  
  37. {  
  38.     return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),  
  39.     (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));  
  40. }   
  41.   
  42. void print(const complex &c)  
  43. {  
  44.     if(c.imag<0)  
  45.         cout<<c.real<<c.imag<<'i'<<endl;  
  46.     else  
  47.         cout<<c.real<<'+'<<c.imag<<'i'<<endl;  
  48. }   
  49.   
  50. void main()  
  51. {  
  52.     complex c1(2.0, 3.0), c2(4.0, -2.0), c3;  
  53.     c3 = c1 + c2;  
  54.     cout<<"c1+c2=";  
  55.     print(c3);  
  56.     c3 = c1 - c2;  
  57.     cout<<"c1-c2=";  
  58.     print(c3);  
  59.     c3 = c1 * c2;  
  60.     cout<<"c1*c2=";  
  61.     print(c3);  
  62.     c3 = c1 / c2;  
  63.     cout<<"c1/c2=";  
  64.     print(c3);  
  65.     c3 = (c1+c2) * (c1-c2) * c2/c1;  
  66.     cout<<"(c1+c2)*(c1-c2)*c2/c1=";  
  67.     print(c3);  
  68.     cout<<endl;  
  69. }   

该程序的运行结果为: 

    

 

   2. 重载为友元函数

         运算符重载函数还可以为友元函数。当重载为友元函数时,将没有隐含的参数this指针。这样,对双目运算符,友元函数有2个参数,对单目运算符,友元函数有一个参数。但是,有些运行符不能重载为友元函数,它们是:=,(),[]和->。

  重载为友元函数的运算符重载函数的定义格式如下: 

  friend <类型说明符> operator <运算符>(<参数表>)
  {……} 

  下面用友元函数代码成员函数,重载编写上述的例子,程序如下:

[cpp] view plaincopy
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class complex  
  5. {  
  6.     public:  
  7.         complex() { real=imag=0; }  
  8.         complex(double r, double i)  
  9.         {  
  10.             real = r, imag = i;  
  11.         }  
  12.         friend complex operator +(const complex &c1, const complex &c2);//运算符重载函数申明为友元函数,参数为两个complex类对象  
  13.         friend complex operator -(const complex &c1, const complex &c2);  
  14.         friend complex operator *(const complex &c1, const complex &c2);  
  15.         friend complex operator /(const complex &c1, const complex &c2);  
  16.         friend   
  17.     void print(const complex &c);  
  18.         private:  
  19.         double real, imag;  
  20. };   
  21.   
  22. complex operator +(const complex &c1, const complex &c2)//友元函数定义,独立函数,不是complex的成员函数  
  23. {  
  24.     return complex(c1.real + c2.real, c1.imag + c2.imag);  
  25. }   
  26.   
  27. complex operator -(const complex &c1, const complex &c2)  
  28. {  
  29.     return complex(c1.real - c2.real, c1.imag - c2.imag);  
  30. }   
  31.   
  32. complex operator *(const complex &c1, const complex &c2)  
  33. {  
  34.     return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);  
  35. }   
  36.   
  37. complex operator /(const complex &c1, const complex &c2)  
  38. {  
  39.     return complex((c1.real * c2.real + c1.imag + c2.imag) / (c2.real * c2.real + c2.imag * c2.imag),  
  40.     (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));  
  41. }   
  42.   
  43. void print(const complex &c)  
  44. {  
  45.     if(c.imag<0)  
  46.         cout<<c.real<<c.imag<<'i'<<endl;  
  47.     else  
  48.         cout<<c.real<<'+'<<c.imag<<'i'<<endl;  
  49. }   
  50.   
  51. void main()  
  52. {  
  53.     complex c1(2.0, 3.0), c2(4.0, -2.0), c3;  
  54.     c3 = c1 + c2;  
  55.     cout<<"c1+c2=";  
  56.     print(c3);  
  57.     c3 = c1 - c2;  
  58.     cout<<"c1-c2=";  
  59.     print(c3);  
  60.     c3 = c1 * c2;  
  61.     cout<<"c1*c2=";  
  62.     print(c3);  
  63.     c3 = c1 / c2;  
  64.     cout<<"c1/c2=";  
  65.     print(c3);  
  66.     c3 = (c1+c2) * (c1-c2) * c2/c1;  
  67.     cout<<"(c1+c2)*(c1-c2)*c2/c1=";  
  68.     print(c3);  
  69.     cout<<endl;  



该程序的运行结果与上例相同。

        前面已讲过,对双目运算符,重载为成员函数时,仅一个参数,另一个被隐含;重载为友元函数时,有两个参数,没有隐含参数。

        因此,程序中出现的     c1+c2

  编译程序解释为:  operator+(c1, c2)

  调用如下函数               complex operator +(const coplex &c1, const complex &c2) 进行求值,

 

二、 两种重载形式的比较

   一般说来,单目运算符最好被重载为成员函数;对双目运算符最好被重载为友元函数,双目运算符重载为友元函数比重载为成员函数更方便些,但是,有的双目运算符还是重载为成员函数为好,例如,赋值运算符。因为,它如果被重载为友元函数,将会出现与赋值语义不一致的地方。

 

三、其他运算符的重载举例

 

   1.下标运算符重载

  由于C语言的数组中并没有保存其大小,因此,不能对数组元素进行存取范围的检查,无法保证给数组动态赋值不会越界。利用C++的类可以定义一种更安全、功能强的数组类型。为此,为该类定义重载运算符[]。

  下面先看看一个例子:

[cpp] view plaincopy
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class CharArray  
  5. {  
  6.     public:  
  7.         CharArray(int l)  
  8.         {  
  9.             Length = l;  
  10.             Buff = new char[Length];  
  11.         }  
  12.         ~CharArray() { delete Buff; }  
  13.         int GetLength() { return Length; }  
  14.         char & operator [](int i);  //[]运算符重载函数说明,函数返回值为字符型地址(指针)  
  15.     private:  
  16.         int Length;  
  17.         char * Buff;  
  18. };   
  19.   
  20. char & CharArray::operator [](int i) //在外部定义类CharArray的成员函数  
  21. {  
  22.     static char ch = 0; //0是字符NULL的ASCII码  
  23.     if(i<Length&&i>=0)        //数组下标边界检查,C++中的数组下标从0开始  
  24.         return Buff[i]; //返回第i个数组元素的地址  
  25.     else  
  26.     {  
  27.         cout<<"Index out of range."<<endl;  
  28.         return ch;  //返回空指针null  
  29.     }  
  30. }   
  31.   
  32. void main()  
  33. {  
  34.     int cnt;  
  35.     CharArray string1(6);  
  36.     char * string2 = "string";  
  37.     for(cnt=0; cnt<8; cnt++)  
  38.         string1[cnt] = string2[cnt];  
  39.     cout<<endl;  
  40.     for(cnt=0; cnt<8; cnt++)  
  41.         cout<<string1[cnt];  
  42.     cout<<endl;  
  43.     cout<<string1.GetLength()<<endl;  
  44. }   

运行结果:

该数组类的优点如下:

  (1) 其大小不必是一个常量。可以先定义一个变量i,然后用CharArray string1(i);定义

  (2) 运行时动态指定大小可以不用运算符new和delete。

  (3) 当使用该类数组作函数参数时,不必分别传递数组变量本身及其大小,因为该对象中已经保存大小。

  在重载下标运算符函数时应该注意:

  (1) 该函数只能带一个参数,不可带多个参数。

     (2) 不得重载为友元函数,必须是非static类的成员函数。

 

   2. 重载增1减1运算符

  增1减1运算符是单目运算符。它们又有前缀和后缀运算两种。为了区分这两种运算,将后缀运算视为双目运算符。表达式

  obj++或obj--

  被编译器看作为:

  obj++0或obj--0

  下面举一例子说明重载增1减1运算符的应用。

[cpp] view plaincopy
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class counter  
  5. {  
  6.     public:  
  7.         counter() { v=0; }  
  8.         counter operator ++();  //前++运算符重载函数  
  9.         counter operator ++(int );  //后++运算符重载函数  
  10.         void print() { cout<<v<<endl; }  
  11.     private:  
  12.         unsigned v;  
  13. };   
  14.   
  15. counter counter::operator ++()  //前++运算符重载函数外部定义  
  16. {  
  17.     v++;  
  18.     return *this;  
  19. }   
  20.   
  21. counter counter::operator ++(int)   //后++运算符重载函数外部定义  
  22. {  
  23.     counter t;  
  24.     t.v = v++;  
  25.     return t;  
  26. }   
  27.   
  28. void main()  
  29. {  
  30.     counter c;  
  31.     for(int i=0; i<8; i++)  
  32.         c++;  
  33.     c.print();  
  34.     for(int i=0; i<8; i++)  
  35.         ++c;  
  36.     c.print();  
  37. }   

运行结果:

 

   3. 重载函数调用运算符

  可以将函数调用运算符()看成是下标运算[]的扩展。函数调用运算符可以带0个至多个参数。下面通过一个实例来熟悉函数调用运算符的重载。 
[cpp] view plaincopy
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class F  
  5. {  
  6.     public:  
  7.         double operator ()(double x, double y) const;//()运算符重载函数  
  8. };   
  9.   
  10. double F::operator ()(double x, double y) const//()运算符重载函数外部定义  
  11. {  
  12.     return (x+5)*y;  
  13. }   
  14.   
  15. void main()  
  16. {  
  17.     F f;  
  18.     cout<<f(1.5, 2.2)<<endl;  
  19. }   

运行结果:

更多0


原创粉丝点击