重载运算符

一、编写MyString类

对于下面的类MyString,要求重载一些运算符后可以计算表达式a=b+c。

其中,a、b、c都是类MyString的对象。请重载相应的运算符并编写程序测试

[cpp]  view plain  copy
  1. class MyString  
  2. {  
  3.     char* str;  
  4. public:  
  5.     MyString(char* s)  
  6.     {  
  7.         str = new char(strlen(s)+1);  
  8.         strcpy(str, s);  
  9.     }  
  10.     ~MyString()  
  11.     {  
  12.         delete []str;  
  13.     }  
  14. };  
解析:

  为了实现a=b+c这个表达式,需要重载两个运算符,一个是‘+’,一个是‘=’,程序如下:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <string.h>  
  3.   
  4. using namespace std;  
  5.   
  6. class MyString  
  7. {  
  8.     char* str;  
  9. public:  
  10.     MyString(char* s)  
  11.     {  
  12.         str = new char(strlen(s)+1);  
  13.         strcpy(str, s);  
  14.     }  
  15.     ~MyString()  
  16.     {  
  17.         delete []str;  
  18.     }  
  19.       
  20.     MyString & operator = (MyString &string)                        //重载=  
  21.     {  
  22.         if(this == &string)  
  23.         {  
  24.             return *this;  
  25.         }  
  26.         if(str != NULL)  
  27.         {  
  28.             delete []str;  
  29.         }  
  30.         str = new char[strlen(string.str)+1];  
  31.         strcpy(str, string.str);  
  32.         return *this;  
  33.     }  
  34.       
  35.     MyString & operator + (MyString &string)                        //重载+  (改变被加对象)  
  36.     {  
  37.         char* temp = str;  
  38.         str = new char[strlen(temp) + strlen(string.str) + 1];  
  39.         strcpy(str, temp);  
  40.         delete []temp;  
  41.         strcat(str, string.str);  
  42.         return *this;  
  43.     }  
  44.     /* 
  45.     MyString & operator + (MyString & string)                       //重载+  (不改变被加对象) 
  46.     { 
  47.         MyString *pstring = new MyString(""); 
  48.         pstring->str = new char[strlen(str)+strlen(string.str)+1]; 
  49.         strcpy(pstring->str, str); 
  50.         strcat(pstring->str, string->str); 
  51.         return *pstring; 
  52.     }*/  
  53.       
  54.     void print()  
  55.     {  
  56.         cout<<str<<endl;  
  57.     }  
  58. };  
  59. /* 
  60. //MyString类的友元,要求str成员是public权限 
  61. MyString &operator + (MyString& right, MyString& left) 
  62. { 
  63.     MyString* pstring = new MyString(""); 
  64.     pstring->str = new char[strlen(right.str)+strlen(left.str)+1]; 
  65.     strcpy(pstring->str, left.str); 
  66.     strcat(pstring->str, right.str); 
  67.      
  68.     return *pstring; 
  69. }*/  
  70.   
  71. int main()  
  72. {  
  73.     MyString a("hello ");  
  74.     MyString b("world");  
  75.       
  76.     MyString c("");  
  77.     c = c + a;  
  78.     c.print();  
  79.       
  80.     c = c + b;  
  81.     c.print();  
  82.       
  83.     c = a + b;  
  84.     a.print();  
  85.     c.print();  
  86.           
  87.     return 0;  
  88. }  
  这里有三个版本的‘+’操作符重载函数,它们都是调用strcpy复制第一个字符串,然后调用strcat连接两个字符串。

  第一个版本返回*this对象,它改变了被加对象的内容。使用第一个‘+’操作符重载函数版本的执行结果:

[cpp]  view plain  copy
  1. hello   
  2. hello world  
  3. hello world      (对象a的str成员被改变了)  
  4. hello world  
  第二个版本和第三个版本都是返回堆中构造的对象,它们没有改变被加对象内容。它们的区别如下:

(1)第二个版本属于类的成员函数,而第三个版本是类的友元函数。

(2)第二个版本的参数为1个,而第三个版本的参数为2个,因为友元函数不含有this指针。

(3)由于类的友元函数不能使用私有成员,因此在这里使用第三个版本时需要把str成员的访问权限改为public。

使用这两个操作符重载函数版本的结果:

[cpp]  view plain  copy
  1. hello   
  2. hello world  
  3. hello        (对象a的str成员没有被改变)  
  4. hello world  
选择何种版本的‘+’操作符重载函数要取决于实际情况。

二、new操作符重载的使用

下面程序中主函数的new是类中new操作符重载。但是new后面只有一个参数0xa5,而类中函数的声明有两个参数。怎么调用这个类呢?

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <malloc.h>  
  3. #include <memory.h>  
  4.   
  5. using namespace std;  
  6.   
  7. class Blanks  
  8. {  
  9. public:  
  10.     void* operator new(size_t stallocateBlock, char chInit);  
  11. };  
  12.   
  13. void* Blanks::operator new(size_t stallocateBlock, char chInit)  
  14. {  
  15.     void* pvTemp = malloc(stallocateBlock);  
  16.     if(pvTemp != 0)  
  17.     {  
  18.         memset(pvTemp, chInit, stallocateBlock);  
  19.     }  
  20.     return pvTemp;  
  21. }  
  22.   
  23. int main()  
  24. {  
  25.     Blanks *a5 = new(0xa5) Blanks;  
  26.           
  27.     return 0;  
  28. }  
这里有一下几点需要说明:

(1)重载new操作符第一个参数必须是size_t类型,并且传入的值就是类的大小。

(2)代码25行中的0xa5表示第二个参数的大小,也就是chInit为0xa5.

(3)代码18行,用chInit初始化分配的那块内存。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值