0
I have problem with operator overloading
I have a code in which i have overloaded addition operator and assignment operator for a class but the problem is this that the below code executes //code open ob1+ob2=ob3; //code close but as the sum of two objects should be a constant and that's why couldn't be assigned another object but the statement is executing in my code although the values they get are not reasonable but according to me the code shouldn't work. it would be very helpful if someone knows how to stop this line from executing and give an error as it happens with //code open int a, b, c; a+b=c; //code close the above code will result into error but my overloading operators doesn't let this happen , please suggest me something .
3 Answers
+ 1
It is legal to do. ob1+ob2 makes a temporary ob4. ob4.operator=(ob3) is a valid method call. See this for a more detailed discussion:
https://stackoverflow.com/questions/25393987
+ 1
No. If int was a class, it would work there too.
0
but do you have any way to stop this
as it happens with normal integer data types