A friend of mine works as a programmer and he often argues with an employee about the fact that if(a==b) is the same as if(b==a) . Is it so?
7 answers
I'm not sure, but I think it all depends on the classes a and b - more precisely, on the implementation of the comparison operator == . For example, in class A it always returns true , in class B it is false . Then the result is a == b and b == a will be different.
- 3Overloaded operators are rare villains. For this, you need to shoot silver bullets .. rubbed with garlic :) - cy6erGn0m
- onecompetently overloaded operators facilitate some actions - Specter
No, not always, a simple example:
std::string s = "a"; if (s == "a"){ doSomething(); // works } if ("a" == s){ doAnother(); // does not work } In general, the result of evaluating the expression a == b in C and C ++ is not equal to the result of the expression b == a. For example, a and b can be macros:
#define a 0 || 1 #define b 1 && 0 Then two expressions are obtained whose calculation results are not equal.
(0 || 1 == 1 && 0) != (1 && 0 == 0 || 1) because the == operator has a higher priority than the && and || operators.
- fourYou have shown an excellent example of how macros cannot be written in any way. The correct option is to enclose the macro in brackets. Those. <code> #define a (0 ¦¦ 1) </ code> - gecube
- 3Not quite on the topic, you would still in the quality of a and b take expressions that change their operands. - avp
For built-in types and pointers, a == b and b == a is always the same, for structures or classes it depends on the implementation of operator ==. The == operator must be symmetric, as they usually do. If this is not the case, then most likely this is a mistake, or the class creator wanted to confuse everyone.
Yes, yes.
Although, of course, if a and b different types, then there may be a collision when casting to the same type, for example:
int a = 8; float b = 8.12381211; if (a == (int)b) printf("a == b"); if (b == (float)a) printf("b == a "); In this case, only the first condition will be satisfied.
- oneAbout the conflict I agree. It all depends strongly on whether types are reducible to each other. If not, I think that the signatures of operator functions for the pairs (a, b) and (b, a) will be different ... And, therefore, the result of the execution can also be different. True, I can not imagine why this may be necessary. - gecube
- This usually occurs quite by accident when defining comparison operations. And then debug a long time :) - KoVadim
- I have MinGW Windows XP gcc and g ++ both are not executed (as one would expect according to the rules for casting in C, C ++). - avp
If a and b are just variable names, then in C (I don’t know about pluses) if (a == b) will almost always give the same result as if (b == a).
Almost applies to the case when between two if handles a signal handler (or thread) that changes the value of variables.
Usually it makes no difference. It is for this reason that for newbies there is a recommendation to write, say, not if (a == 0); a if (0 == a), so that they do not confuse = and ==. In general, I wonder if someone compares the type of float with the type of int? Usually operate on variables of the same type.
- Well, for equality may not compare as different types as int and float, then more or less compare exactly. But this does not change the essence of working with operators. The recommendation, by the way, does not work if there are variables on both sides of the == operator. In the case of a constant, yes, it is possible to catch the error. - gecube
- If all variables are initialized, the order does not matter. The recommendation is given only for beginners, so that errors are caught during compilation . - 3JIoi_Hy6 5:46 pm