复合赋值和添加运算符重载 [英] Compound assignment and add operator overloading

查看:150
本文介绍了复合赋值和添加运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助我的两个运算符重载函数如下所示。

I need help with both of my operator overloading functions presented below. I'm unsure of how I can implement this without actually using the assignment in the function definitions.

我的.cpp文件中的操作符+的代码:

Code for operator + in my .cpp file:

MyString& MyString::operator +(const MyString& rhs)
{
  delete [] String;
  String = new char[rhs.Size];
  Size = rhs.Size;
  // needs to be a loop for cascading + 
  // so that String1=String2+String3+String4 will work
  for(int i = 0; i < rhs.Size+1 ; i++)
  {
    //  String[i] + rhs.String[i]; ???
  }
  return *this;
}

.cpp文件中+操作符的代码:

Code for += operator in .cpp file:

MyString& MyString::operator+=(const MyString& rhs)
{
  delete [] String;
  String = new char[rhs.Size];
  String = String + rhs.String;
  return *this;
}

从main.cpp调用:

Call from main.cpp:

 String1 = String2 + String3 + String4;
 String1.Print ();

 String2 += String3;
 String2.Print ();

我知道我的.cpp文件的代码是错误的,一些洞察力是非常好的!

I know my .cpp file codes are wrong, some insight would be great!

推荐答案

首先,通常从 operator + 返回一个新对象, + on一个对象不改变对象本身。

First, usually you return a new object from operator+, because the expectation is that calling + on an object does not change the object itself.

MyString MyString::operator+ (const MyString& rhs)  
{  
  // ...

  return MyString(...);  

} 

请注意缺少的引用(& ;

Note the missing reference (&) from the return type: you are returning the new object by-copy, not by-reference.

其次,如果你<$>

char* tmp = new char[Size + rhs.Size + 1]; // +1 for the terminating '\0'      
for(int i = 0; i < Size ; i++)      
{
  // copy the contents of current object buffer, char-by-char
  tmp[i] = String[i]; 
}
for(int i = 0; i < rhs.Size+1; i++) // +1 to copy the terminating '\0' as well
{      
  // copy the contents of other object buffer, char-by-char
  tmp[i+Size] = rhs.String[i]; 
}
MyString result;
delete[] result.String;
result.String = tmp;
result.Size = Size+rhs.Size;      

return result;

operator + = 因为你需要操作当前对象的缓冲区:

operator+= is a bit more tricky, because you need to manipulate the current object's buffer:

char* tmp = new char[Size + rhs.Size + 1]; // +1 for the terminating '\0'      
for(int i = 0; i < Size ; i++)      
{      
  tmp[i] = String[i]; 
}
for(int i = 0; i < rhs.Size+1; i++) // +1 to copy the terminating '\0' as well
{      
  tmp[i+Size] = rhs.String[i]; 
}
delete[] String;
String = tmp;
Size += rhs.Size;      

return *this;

更新:我假设你还调用 delete [] 在类析构函数 - 你应该。也不难想象,你会想要从一个 MyString 对象到另一个对象执行赋值。这将导致cosider 三条规则:如果您需要任何一个desgtructor,复制构造函数或赋值运算符,你很可能需要所有三个。

Update: I assume you also call delete[] in the class destructor -- which you should. It is also not hard to imagine, you will want to perform assingment from one MyString object to another. This will lead to cosider the rule of three: If you need any one of desgtructor, copy-constructor or assignment operator, you most likely need all three.

这篇关于复合赋值和添加运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆