### Is this a good start?

Pages: 12
Okay thanks I will try other methods to improve the performance an easy fix that I can think of would be to just add to the end then reverse it for the final answer would that be better or should I look for a better performance method?

*edit also I don't like how my for loop looks I am going to fix that so it isn't so rubbish.
Last edited on
 a+b *= 2
is nonsense any way you look at it.

 (a+b).duplicate()
is a valid rvalue, and you know it.
Well I have come up with this now but I don't think its much better I gotta think of a better way where I don't need to reverse the string.

 1234567891011121314151617181920212223242526272829 Integer& Integer::operator+=( Integer i ) { std::string temp( "" ); unsigned short remainder( 0 ) , carry( 0 ); while( !value.empty() && !i.value.empty() ) { remainder = value.back() - '0' + i.value.back() - '0' + carry; value.pop_back(); i.value.pop_back(); if( remainder > 9 ) { carry = 1; remainder -= 10; } else { carry = 0; } temp += remainder + '0'; } temp += value += i.value; std::reverse( temp.begin() , temp.end() ); value = temp; return( *this ); }

Edit*
Here is another method are either of these better than the orig?

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 Integer& Integer::operator+=( Integer i ) { std::string temp( "" ) , extra( "" ); unsigned short remainder( 0 ) , carry( 0 ); const bool lhs_greater = value.size() > i.value.size(); while( value.size() != i.value.size() ) { if( lhs_greater ) { extra += value.at( 0 ); value.erase( 0 , 1 ); } else { extra += i.value.at( 0 ); value.erase( 0 , 1 ); } } auto lhs_it = value.rbegin() , rhs_it = i.value.rbegin(); while( lhs_it != value.rend() ) { remainder = *lhs_it - '0' + *rhs_it - '0' + carry; if( remainder > 9 ) { carry = 1; remainder -= 10; } else { carry = 0; } temp += remainder + '0'; ++lhs_it; ++rhs_it; } if( remainder == 1 ) extra.back() + remainder - '0'; std::reverse( temp.begin() , temp.end() ); value = extra + temp; return( *this ); }
Last edited on
Duoas wrote:
a+b *= 2 is nonsense any way you look at it.

(a+b).duplicate() is a valid rvalue, and you know it.
What if I said (a+b).operator*=(2)? It "is a valid rvalue, and you know it", yet it is also "nonsense any way you look at it."
Last edited on
Why is this so confusing?

I know it is possible to do it, but it is nonsensical to ASSIGN VALUE to (a+b), and any compiler worth it's salt ought to tell you you are probably doing something wrong. Why? Because (a+b) is not a proper lvalue.

(Now, I know you can shuffle the shells enough to make (a+b) into some grotesque version of an lvalue, but whatever you do to it will be non-obvious, because it breaks design pattern and KISS.)

In contrast, (a+b) is a perfectly normal rvalue, and there is nothing wrong with applying operations to it.

By definition, whenever you have an EQUALS SIGN, you are playing with an lvalue on the left, and an rvalue on the right.

(And again, I don't care if you can twist C++ syntax into something that invalidates this statement. I can do it too, but I won't, because it would be WRONG.)

So, you want to take two separate statements of mine, mix them, and claim that the false conclusion is my words? That's a straw man.

Go eat your own poo and stop throwing it at me.
Duoas I know what you mean, I am just saying ne555 has a point that you are not clear why one is nonsense and the other isn't. Obviously now you have been clear so there is no problem ;)
Last edited on

I'm simply saying that you wouldn't apply operations that modify the state of a temporary object.
So you would only use functions that are declared as const.