运算符的重载+、-、*、/、[]、自加++的重载

本文详细介绍了运算符重载的概念及应用,通过实例演示了如何重载加法运算符和自增运算符,并解释了哪些运算符可以重载以及重载的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.为什么要用到运算符的重载

所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作。运算符重载(Operator Overloading)也是一个道理,同一个运算符可以有不同的功能.


看下面这个代码:怎样实现虚数的加减。
[cpp]  view plain  copy
  1. <pre code_snippet_id="2491281" snippet_file_name="blog_20170720_1_6836729" name="code" class="cpp">#include <iostream>  
  2. using namespace std;  
  3.   
  4. class array{  
  5. public:  
  6.     array();  
  7.     array(int arr[]);  
  8.     array operator + (const array a)const;  
  9.     void display()const;  
  10. private:  
  11.     int m_arr[5];  
  12. };  
  13.   
  14. array :: array()  
  15. {  
  16.     for (int  i = 0; i < 5; i++)  
  17.     {  
  18.         m_arr[i] = 0;  
  19.     }  
  20. }  
  21.   
  22. array :: array(int arr[])  
  23. {  
  24.     for (int i = 0; i < 5; i++)  
  25.     {  
  26.         m_arr[i] = arr[i];  
  27.     }  
  28. }  
  29.   
  30. array array :: operator + (const array a)const  
  31. {  
  32.     array b;  
  33.     for (int i = 0; i < 5; i++)  
  34.     {  
  35.         b.m_arr[i] = this->m_arr[i] + a.m_arr[i];  
  36.     }  
  37.     return b;  
  38. }  
  39.   
  40. void array :: display()const  
  41. {  
  42.     for (int i = 0; i < 5; i++)  
  43.     {  
  44.         cout<<m_arr[i]<<" ";  
  45.     }   
  46. }  
  47.   
  48. int main()  
  49. {  
  50.     int a[5] = {1, 2, 3, 4, 5};  
  51.     int b[5] = {1, 2, 3, 4, 5};  
  52.     array obja(a);  
  53.     array objb(b);  
  54.     array objc;  
  55.     objc = obja + objb;  
  56.     objc.display();  
  57.       
  58.     return 0;  
  59. }</pre><br>  
  60. <pre></pre>  
  61. 运算符重载的格式为:  
返回值类型  operator 运算符名称(形参列表)
{
        //函数体
}
operator是关键字,专门用于定义重载运算符的函数。将operator 运算符名称这一部分看做函数名,对于上面的代码,函数名就是operator+。
运算符重载函数除了函数名有特定的格式,其它地方和普通函数并没有区别。

上面的例子中,我们在 array类中重载了运算符+,该重载只对 array对象有效。当执行
[cpp]  view plain  copy
  1. objc = obja + objb;<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">语句时,编译器检测到+号左边(+号具有左结合性,所以先检测左边)是一个 array 对象,就会调用成员函数operator+(),也就是转换为下面的形式:</span>  
[cpp]  view plain  copy
  1. objc = obja.operator+ objb   
[cpp]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">obja 是要调用函数的对象,objb 是函数的实参。</span>  

2.并不是所有的运算符都能够重载

能够重载的运算符包括:
+  -  *  /  %  ^  &  |  ~  !  =  <  >  +=  -=  *=  /=  %=  ^=  &=  |=  <<  >>  <<=  >>=  ==  !=  <=  >=  &&  ||  ++  --  ,  ->*  ->  ()  []  new  new[]  delete  delete[]
上述运算符中,[]是下标运算符,()是函数调用运算符。自增自减运算符的前置和后置形式都可以重载。
长度运算符sizeof、条件运算符: ?、成员选择符.和域解析运算符::不能被重载。

3.重载不能改变运算符的优先级和结合性

例如:c4 = c1 + c2 * c3;
等价于:
c4 = c1 + ( c2 * c3 );
乘法的优先级仍然高于加法,并且它们仍然是二元运算符。

4.重载不会改变运算符的用法,原有有几个操作数、操作数在左边还是在右边,这些都不会改变。例如~号右边只有一个操作数,+号总是出现在两个操作数之间,重载后也必须如此。

5.运算符重载函数既可以作为类的成员函数,也可以作为全局函数。

6.箭头运算符->、下标运算符[ ]、函数调用运算符( )、赋值运算符=只能以成员函数的形式重载。

举个例子吧:下面这个重载自加运算符的代码
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <iomanip>  
  3. using namespace std;  
  4.   
  5. class watch{  
  6. public:  
  7.     watch():m_min(0), m_sec(0){}  
  8.     void setzero();  
  9.     watch run();  //返回值是watch类型 是一个类  
  10.     watch operator ++ ();  // ++i  
  11.     watch operator ++ (int n); //i++   n无意义,只是来区别++i  
  12.     friend ostream & operator << (ostream &, const watch &);  
  13. private:  
  14.     int m_min;  
  15.     int m_sec;  
  16. };  
  17.   
  18. void watch :: setzero()  
  19. {  
  20.     m_min = 0;  
  21.     m_sec = 0;  
  22. }  
  23.   
  24. watch watch :: run()  
  25. {  
  26.     ++m_sec;  
  27.     if (60 == m_sec)  
  28.     {  
  29.         m_min++;  
  30.         m_sec = 0;  
  31.     }  
  32.     return *this;  
  33. }  
  34.   
  35. watch watch :: operator ++ ()  
  36. {  
  37.     return run();  
  38. }  
  39.   
  40. watch watch :: operator ++ (int n)  
  41. {  
  42.     watch t = *this;  
  43.     run();  
  44.     return t;  
  45. }  
  46.   
  47. ostream & operator << (ostream & out, const watch & t)  
  48. {  
  49.     out<<setfill('0')<<setw(2)<<t.m_min<<":"<<setw(2)<<t.m_sec;  
  50.     return out;  
  51. }  
  52.   
  53. int main()  
  54. {  
  55.       
  56.     watch t1, t2;  
  57.       
  58.     t1 = t2++;  
  59.     cout<<"t1:"<<t1<<endl;  
  60.     cout<<"t2:"<<t2<<endl;  
  61.     t1.setzero();  
  62.     t2.setzero();  
  63.       
  64.     t1 = ++t2;  
  65.     cout<<"t1:"<<t1<<endl;  
  66.     cout<<"t2:"<<t2<<endl;  
  67.     t1.setzero();  
  68.     t2.setzero();  
  69.       
  70.     return 0;  
  71. }  

运行结果:
t1:00:00
t2:00:01
t1:00:01
t2:00:01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值