C++回顾之前置++、后置++、不等号!及赋值运算符重载

http://blog.csdn.net/ab198604/article/details/19827777

运算符重载的主要目的是为了让类对象能像普通数据类型一样能够进行加减乘除,自加自减等操作,非常直观方便。现在来回顾C++的自加减(分前置与后置)以及不等号非运算符,赋值运算符的重载。

        1 ++重载

        (1)前置++运算符的重载方式:

        成员函数的重载: 函数类型& operator++()

        友元函数的重载:friend 函数类型& operator++(类类型& )


        (2)后置++运算符的重载方式:

        成员函数的重载:函数类型& operator++(int)

        友元函数的重载:friend 函数类型& operator++(类类型&, int)


        注意,为了区分前置++与后置++的区别,需要在参数后增加一个"int"以示区分。含有"int"的重载方式为后置++,否则为前置++。前置--与后置--类似用法。前面说过,成员函数与友元函数的重载如果同时存在时,会先调用成员函数的重载,但是在++或--时,成员函数与友元函数的重载是不能同时存在的。

        下面举一个例子:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef _INTEGER_H_  
  2. #define _INTEGER_H_  
  3.   
  4. class Integer  
  5. {  
  6. public:  
  7.     Integer(int n);  
  8.     ~Integer();  
  9.     void Display();  
  10.   
  11.     Integer& operator++(); //成员方式重载前置++  
  12.     Integer& operator++(int); //成员方式重载后置++  
  13.   
  14.     friend Integer& operator++(Integer& i);//友元函数重载前置++  
  15.     friend Integer& operator++(Integer& i, int);//友元函数重载后置++  
  16.   
  17.       
  18. private:  
  19.     int n_;  
  20. };  
  21.   
  22. #endif  
  

        下面是它们的具体代码实现:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "Integer.h"  
  2.   
  3. Integer::Integer(int n):n_(n){}  
  4.   
  5. Integer::~Integer(){}  
  6.   
  7. void Integer::Display() const  
  8. {  
  9.      cout << n_ << endl;  
  10. }  
  11.   
  12. //最好优先使用成员函数重载,  
  13.   
  14. //成员函数重载前置++  
  15. Integer& Integer::operator++()  
  16. {  
  17.     ++n_;  
  18.     return *this;  
  19. }  
  20.   
  21. //成员函数重载后置++  
  22. Integer& Integer::operator++(int)  
  23. {  
  24.     Integer tmp(n_);  
  25.     ++n_;  
  26.     return tmp;  
  27. }  
  28.   
  29. //友元重载前置++  
  30. Integer& operator++(Integer& i)  
  31. {  
  32.     ++i.n_;  
  33.     return i;  
  34. }  
  35.   
  36. //友元重载后置++  
  37. Integer& operator++(Integer& i, int)  
  38. {  
  39.     Integer tmp(i.n_);  
  40.     ++i.n_;  
  41.     return tmp;  
  42. }  

        关于!及=赋值运算符的重载以String类进行说明:

         下面是String类的定义:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef STRING_H_  
  2. #define STRING_H_  
  3.   
  4. class String  
  5. {  
  6. public:  
  7.     explicit String(const char *str="");  
  8.     String(const String& other);  
  9.     ~String();  
  10.   
  11.     //应用于s="abc";的情况  
  12.     String& operator=(const char *str);//重载=  
  13.   
  14.     bool operator!() const//重载!  
  15.   
  16.     void Display() const;  
  17.   
  18. private:  
  19.     char* str_;  
  20.     char* AllocAndCopy(const char* str);   
  21. };  
  22.   
  23. #endif  

        下面是具体实现:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <string.h>  
  2. #include <iostream>  
  3. #include "String.h"  
  4. using namespace std;  
  5.   
  6. String::String(const char *str)  
  7. {  
  8.     str_ = AllocAndCopy(str);  
  9. }  
  10.   
  11. String::~String()  
  12. {  
  13.     delete [] str_;  
  14. }  
  15.   
  16. char* String::AllocAndCopy(const char* str)  
  17. {  
  18.     int len = strlen(str)+1;  
  19.     char* newstr = new char[len];  
  20.     memset(newstr, 0, len);  
  21.     strcpy(newstr, str);  
  22.   
  23.     return newstr;  
  24. }  
  25.   
  26.   
  27. //深拷贝,copy assign  
  28. String& String::operator=(const String& other)   //s1 = s2  
  29. {  
  30.     ifthis == &other)  
  31.         return *this;  
  32.       
  33.     delete [] str_;  
  34.     str_ = AllocAndCopy(other.str_);  
  35.     return *this;  
  36. }  
  37.   
  38. String& String::operator=(const char* str)  //s1="abc"  
  39. {  
  40.     delete []  str_;  
  41.     str_ = AllocAndCopy(str);  
  42.     return *this;  
  43. }  
  44.   
  45. bool String::operator!() const  
  46. {  
  47.     return (strlen(str_) != 0 )  
  48. }  
  49.   
  50. void String::Display() const  
  51. {  
  52.     cout << str_ << endl;  
  53. }  

        针对String类写的一个简单的用例:

        

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "String.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     String s1("abc");  
  8.     String s2(s1);//copy.  
  9.     String s3;//带默认参数  
  10.       
  11.     s3 = s1; //调用赋值运算符  
  12.     s3.Display();  
  13.   
  14.   
  15. //默认的处理方式为:将字符串构造成String类对象,再赋值至s3,如果在构造函数前加上explicit声明,则会发生编译错误。解决的方式需要重载一个参数为const char *的等号运算符即可  
  16.     s3 = "xxx"//调用参数为const char *的赋值运算符  
  17.       
  18.     String s4;  
  19.     bool notempty;  
  20.     notempty = !s4;   
  21.     cout << notempty << endl; //1  
  22.   
  23.     s4 = "aaa";  
  24.     notempty = !s4;  
  25.     cout << notempty << endl; //0  
  26.   
  27.     return 0;  
  28.       
  29. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值