I have created a custom BigInt class and overrode the +
operator to add two objects of this class. But why does C++ asks me to override the shorthand notation +=
as well? And for some reason the shorthand notation doesn't work as expected even if I do override it. What's wrong here?
class BigInt {
string num;
public:
string number() {
return num;
}
BigInt(string n):num(n) {}
BigInt operator+(BigInt o) {
string result;
// logic for adding but not relevant here
return BigInt(result);
}
BigInt operator+=(BigInt o) {
return *this+o;
}
};
from another function for example:
BigInt a(num1);
BigInt b(num2);
a+=b; // doesn't works; it adds but doesn't assign the result back to a
a=a+b; // works; adds as well as result is assigned to a