This question already has an answer here:
这个问题在这里已有答案:
- Uses of C comma operator [duplicate] 20 answers
- C逗号运算符的使用[重复] 20个答案
I had this problem when I accidentally deleted the method name. The code went from
我不小心删除了方法名称时遇到了这个问题。代码来自
bool bRet = MethodName(pData, pOutFilename);
to
至
bool bRet = (pData, pOutFilename);
but still compiled? What does this code do? What does it mean? It seems to return true, is this always the case (even if pData is null)?
但仍然编译?这段代码有什么作用?这是什么意思?它似乎返回true,总是这样(即使pData为null)?
Any ideas are welcome!
欢迎任何想法!
3 个解决方案
#1
37
it is the "comma operator" which
它是“逗号运算符”
evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)。
#2
17
Your expression bool bRet = (pData, pOutFilename);
is a valid expression, and it's equivalent to the expression bool bRet = pOutFilename;
你的表达式bool bRet =(pData,pOutFilename);是一个有效的表达式,它等同于表达式bool bRet = pOutFilename;
In bool bRet = (pData, pOutFilename);
, first expression pData
is evaluated, then the second expression pOutFilename
is evaluated, then value of second expression is assigned to bRet
(this is how ,
operator works from left-to-right).
在bool bRet =(pData,pOutFilename);中,计算第一个表达式pData,然后计算第二个表达式pOutFilename,然后将第二个表达式的值赋给bRet(这是操作符从左到右的方式)。
Read: Comma Operator: ,
阅读:逗号运营商:,
The comma operator
,
hasleft-to-right associativity
. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.逗号运算符具有从左到右的关联性。用逗号分隔的两个表达式从左到右进行计算。始终评估左操作数,并在评估右操作数之前完成所有副作用。
To understand the importance of parenthesis ( )
in your expression, consider my example below. Observe the output in this example (I have C example):
要了解括号()在表达式中的重要性,请考虑下面的示例。观察这个例子中的输出(我有C例子):
int main () {
int i = 10, b = 20, c= 30;
i = b, c; // i = b
printf("%i\n", i);
i = (b, c); // i = c
printf("%i\n", i);
}
output:
输出:
20
30
To understand the output: look at precedence table ,
have lower precedence than =
. In you expression you have overwrite the precedence using parenthesis.
要理解输出:查看优先级表,优先级低于=。在表达式中,您使用括号覆盖优先级。
#3
4
Its a ,
comma operator. If you have an expression like this:
它是一个逗号运算符。如果你有这样的表达式:
i = (a, b);
b
will be stored into i
.
b将存储到i中。
So in your case:
所以在你的情况下:
bRet = pOutFilename;
pOutFilename
will be stored into bRet
.
pOutFilename将存储到bRet中。
#1
37
it is the "comma operator" which
它是“逗号运算符”
evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).
计算其第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)。
#2
17
Your expression bool bRet = (pData, pOutFilename);
is a valid expression, and it's equivalent to the expression bool bRet = pOutFilename;
你的表达式bool bRet =(pData,pOutFilename);是一个有效的表达式,它等同于表达式bool bRet = pOutFilename;
In bool bRet = (pData, pOutFilename);
, first expression pData
is evaluated, then the second expression pOutFilename
is evaluated, then value of second expression is assigned to bRet
(this is how ,
operator works from left-to-right).
在bool bRet =(pData,pOutFilename);中,计算第一个表达式pData,然后计算第二个表达式pOutFilename,然后将第二个表达式的值赋给bRet(这是操作符从左到右的方式)。
Read: Comma Operator: ,
阅读:逗号运营商:,
The comma operator
,
hasleft-to-right associativity
. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.逗号运算符具有从左到右的关联性。用逗号分隔的两个表达式从左到右进行计算。始终评估左操作数,并在评估右操作数之前完成所有副作用。
To understand the importance of parenthesis ( )
in your expression, consider my example below. Observe the output in this example (I have C example):
要了解括号()在表达式中的重要性,请考虑下面的示例。观察这个例子中的输出(我有C例子):
int main () {
int i = 10, b = 20, c= 30;
i = b, c; // i = b
printf("%i\n", i);
i = (b, c); // i = c
printf("%i\n", i);
}
output:
输出:
20
30
To understand the output: look at precedence table ,
have lower precedence than =
. In you expression you have overwrite the precedence using parenthesis.
要理解输出:查看优先级表,优先级低于=。在表达式中,您使用括号覆盖优先级。
#3
4
Its a ,
comma operator. If you have an expression like this:
它是一个逗号运算符。如果你有这样的表达式:
i = (a, b);
b
will be stored into i
.
b将存储到i中。
So in your case:
所以在你的情况下:
bRet = pOutFilename;
pOutFilename
will be stored into bRet
.
pOutFilename将存储到bRet中。