==运算符和操作数 [英] == Operator and operands
问题描述
我要检查一个值是否等于1.下面的代码行是否有任何区别
I want to check whether a value is equal to 1. Is there any difference in the following lines of code
评估值== 1
1 ==评估值
关于编译器执行
推荐答案
在大多数语言中,都是同一回事.
In most languages it's the same thing.
人们经常做1 ==计算值,因为1不是左值.这意味着您不能偶然进行作业.
People often do 1 == evaluated value because 1 is not an lvalue. Meaning that you can't accidentally do an assignment.
示例:
if(x = 6)//bug, but no compiling error
{
}
相反,您可以强制执行编译错误而不是错误:
Instead you could force a compiling error instead of a bug:
if(6 = x)//compiling error
{
}
现在,如果x不是int类型,并且您使用的是C ++之类的东西,那么用户可能已经创建了operator ==(int)覆盖,这使这个问题有了新的含义.在这种情况下,6 == x不会编译,但是x == 6会编译.
Now if x is not of int type, and you're using something like C++, then the user could have created an operator==(int) override which takes this question to a new meaning. The 6 == x wouldn't compile in that case but the x == 6 would.
这篇关于==运算符和操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!