Question: What's wrong with this code? The test case looks like int main(){ { String str(); String str1(); String result = str.operator+=(str1); assert(result == ); }

What's wrong with this code? The test case looks like

int main(){

{

String str("");

String str1("");

String result = str.operator+=(str1);

assert(result == "");

}

{

String str('H');

String str1('i');

String result = str.operator+=(str1);

std::cerr << result;

assert(result == "Hi");

}

}

and the first assert will pass but the second assert for "Hi" will not pass and it says instead of adding H and i the result is only H.

here is the code for the operator+= function.

String& String::operator+=(String rhs){ int offset = length(); int pos = 0; while(rhs.str[pos] != '\0'){ str[offset++] = rhs.str[pos]; ++pos; } str[offset++] = '\0'; reset_capacity(offset); return *this; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!