C++ assignment precedence
But why the result of (haha = 3) is not 1 indicating something like the operation is successful?
Because that is not how the C++ language specification says things works. Instead, the result of an assignment is the value that was assigned. In this case haha = 3
evaluates to 3
.
In C++, we never have "this operation was successful" for the built-in operators. In some cases, the compiler will give an error when you use an operator incorrectly. However, the compiler will just assume you know what you are doing if there is no error that it can find.
Because, when assignment happens, all expression in the right side of the operator needs to be evaluated, then result becomes assigned to variable in the left side of the operator. when evaluating expression hehe = haha = 3
, OS should evaluate haha = 3
first. That is why hehe
equals haha
, and haha
equals 3
.