看源码学C++运算符重载

前两天自己写过有关运算符重载的代码,那只能算得上表面的了解,看了这篇文章中有关的源码,深知自己还有很多不足,转载了。

 

文章转自:http://blog.csdn.net/rao_warrior/article/details/8702804

“什么是运算符的重载”与“为什么要引入运算符重载?”这两个问题,在这里都不多说了,百度一下就都OK了

下面说了一下一些比较容易错的重载,

首先看看模板们是如何声明重载的

下是iterator 模板中的一段源码,大家看看它的格式:

iterator 的一段源码

[cpp] view plain copy print ?
  1. <SPAN style="WHITE-SPACE: pre"> </SPAN>typedef _Vector_iterator<_Ty, _Alloc> _Myt;    reference operator*() const  
  2.         {   // return designated object   
  3.         return ((reference)**(_Mybase *)this);  
  4.         }  
  5.   
  6.     pointer operator->() const  
  7.         {   // return pointer to class object   
  8.         return (&**this);  
  9.         }  
  10.   
  11.     _Myt& operator++()  
  12.         {   // preincrement   
  13.         ++(*(_Mybase *)this);  
  14.         return (*this);  
  15.         }  
  16.   
  17.     _Myt operator++(int)  
  18.         {   // postincrement   
  19.         _Myt _Tmp = *this;  
  20.         ++*this;  
  21.         return (_Tmp);  
  22.         }  
  23.   
  24.     _Myt& operator--()  
  25.         {   // predecrement   
  26.         --(*(_Mybase *)this);  
  27.         return (*this);  
  28.         }  
  29.   
  30.     _Myt operator--(int)  
  31.         {   // postdecrement   
  32.         _Myt _Tmp = *this;  
  33.         --*this;  
  34.         return (_Tmp);  
  35.         }  
  36.   
  37.     _Myt& operator+=(difference_type _Off)  
  38.         {   // increment by integer   
  39.         (*(_Mybase *)this) += _Off;  
  40.         return (*this);  
  41.         }  
  42.   
  43.     _Myt operator+(difference_type _Off) const  
  44.         {   // return this + integer   
  45.         _Myt _Tmp = *this;  
  46.         return (_Tmp += _Off);  
  47.         }  
  48.   
  49.     _Myt& operator-=(difference_type _Off)  
  50.         {   // decrement by integer   
  51.         return (*this += -_Off);  
  52.         }  
  53.   
  54.     _Myt operator-(difference_type _Off) const  
  55.         {   // return this - integer   
  56.         _Myt _Tmp = *this;  
  57.         return (_Tmp -= _Off);  
  58.         }  
  59.   
  60.     difference_type operator-(const _Mybase& _Right) const  
  61.         {   // return difference of iterators   
  62.         return (*(_Mybase *)this - _Right);  
  63.         }  
  64.   
  65.     reference operator[](difference_type _Off) const  
  66.         {   // subscript   
  67.         return (*(*this + _Off));  
  68.         }  


根据上面的源码示例,我们很容易看到:

前缀++(--)与后缀++(--)

1、a++
       函数返回:temp(临时变量)
       函数返回是否是const类型:返回是一个拷贝后的临时变量),不能出现在等号的左边(临时变量不能做左值),函数的结果只能做右值,则要返回一个const类型的值
      ++a
       函数返回:*this;
      函数返回是否是const类型:返回原状态的本身,返回值可以做左值,即函数的结果可以做左值,则要返回一个非const类型的值
2、前后缀仅从函数名(operator++)无法区分,只能有参数区分,这里引入一个虚参数int x,x可以是任意整数。
3、单目运算符的重载:
      重载运算符函数名:operator@(参数表)
      隐式调用形式:obj1@  或 @obj1
      显式调用形式:
             成员函数:
                    obj1.operator@( )//前缀
                    obj1.operator@(0)//后缀
             友元函数:
                    operator@(OBJ obj)//前缀
                    operator@(OBJ obj,int x)//后缀
      执行时,隐式调用形式和显式调用形式都会调用函数operator@()

给出示例出下:

[cpp] view plain copy print ?
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. class Point    
  5. {    
  6. private:    
  7.     int x;   
  8. public:    
  9.     Point(int x1)  
  10.     {   x=x1;}    
  11.     Point& operator++();//成员函数定义自增   
  12.     Point operator++(int x); //后缀可以返回一个const类型的值   
  13. //  Point& operator--();//成员函数定义自增   
  14. //  Point operator--(int x); //后缀可以返回一个const类型的值   
  15.     friend Point& operator--(Point& p);//友元函数定义--   
  16.     friend Point operator--(Point& p,int x);//后缀可以返回一个const类型的值   
  17.   
  18.     friend ostream& operator<<(ostream&out,const Point& p ){  
  19.         out<<p.x;  
  20.         return out ;  
  21.     }  
  22. };    
  23.   
  24. Point& Point::operator++()//++obj   
  25. {  
  26.     x++;  
  27.     return *this;  
  28. }  
  29. Point Point::operator++(int x)//obj++   
  30. {  
  31.     Point temp = *this;  
  32.     this->x++;  
  33.     return temp;  
  34. }  
  35. Point& operator--(Point& p)//--obj   
  36. {  
  37.     p.x--;  
  38.     return p;  
  39.     //前缀形式(--obj)重载的时候没有虚参,通过引用返回*this 或 自身引用,也就是返回变化之后的数值   
  40. }  
  41. Point operator--(Point& p,int x)//obj--   
  42. {  
  43.     Point temp = p;  
  44.     p.x--;  
  45.     return temp;  
  46.     // 后缀形式obj--重载的时候有一个int类型的虚参, 返回原状态的拷贝   
  47. }  
  48. int main(){  
  49.     Point a(1);  
  50.     Point b(2);  
  51.     a++;//隐式调用成员函数operator++(0),后缀表达式   
  52.     ++a;//隐式调用成员函数operator++(),前缀表达式   
  53.     b--;//隐式调用友元函数operator--(0),后缀表达式   
  54.     --b;//隐式调用友元函数operator--(),前缀表达式   
  55.     cout<<a.operator ++(2);//显式调用成员函数operator ++(2),后缀表达式   
  56.     cout<<a.operator ++();//显式调用成员函数operator ++(),前缀表达式   
  57.     cout<<operator --(b,2);//显式调用友元函数operator --(2),后缀表达式   
  58.     cout<<operator --(b);//显式调用友元函数operator --(),前缀表达式 </pre>   
  59.     return 0;  
  60. }  


 

重载输入输出操作符<< >>(另外再强调一下这个)

重载方式:只能使用友元函数重载 且 使用三个引用&
函数名:
       输出流: operator<<(参数表)
       输入流:operator>>(参数表)
参数表:固定(容易出错啊),两个参数均用引用&
       输出流: 必须是两个参数:对输出流ostream& 和 对象
                        第一个操作数cout,定义在文件iostream中,是标准类类型ostream的对象的引用。
                        如:ostream& cout,const Point& p
       输入流:必须是两个参数:对输入流ostream& 和 对象
                       第一个操作数是cin,定义在文件iostream,实际上是标准类类型istream的对象的引用
                       如:instream& cin,const Point& p
函数调用:
       输出流: 显式调用:cout<<对象
                        隐式调用: operator<<(cout,对象)
       输入流:显式调用:cin>>对象
                        隐式调用: operator>>(cin,对象)
返回类型:返回类型固定 + 使用返回函数引用(满足连续输出)
       输出流: 返回ostream&
                        如:ostream& operator<<(ostream& cout,const Point& p)
       输入流:返回:istream&
                        如:istream& operator>>(istream& cin,Point& p)
注意:为什么输入输出操作符的重载必须使用友元函数?
因为:成员函数要求是有对象调用,则第一个参数必须是类的对象,但是<<和>>第一个参数是流的对象引用。故,不能使用成员函数

示例代码如下:

[cpp] view plain copy print ?
  1. class ABC{  
  2.     int data ;  
  3. public:  
  4.     ABC(int d):data(90){    }  
  5.     friend ostream&operator<<(ostream&out,const ABC &o) ;  
  6.     friend istream& operator >>(istream & in, ABC &i);  
  7. };  
  8. ostream &operator<<(ostream&out,const ABC &o){  
  9.     out<<o.data;  
  10.     return out ;  
  11. }  
  12. istream &operator>>(istream& in, ABC &i){  
  13.     in>>i.data ;  
  14.     return in;  
  15. }  


部分内容参考:http://blog.csdn.net/insistgogo/article/details/6626952

你可能感兴趣的:(看源码学C++运算符重载)