简体   繁体   English

这条线返回什么?

[英]What does this line return?

int lf = ((t.left==null) = (t.right==null)) ? 1:0;

如果大括号中的语句为true,则返回1,但是在中间,为lefT分配正确的值有什么意义呢?

Normally you'd have an equal sign to assign. 通常,您需要分配等号。 The return of the assigned is the same as the RHS of the expression. Assigned的返回与表达式的RHS相同。

You'd use an equal sign in a expression within an if to assign and check the result at the same time. 您将在if中的表达式中使用等号来同时分配和检查结果。

// return first and third items added if they exist.
if ((list = GetItems()).Length > 2) { return list[0] + list[2]; } 

Right here all you have is a compiler error because t.left==null evaluates to (true/false) and you can't assign to that. 就在这里,您所拥有的只是一个编译器错误,因为t.left==null计算结果为(true/false) ,您无法为其分配值。

If both t.left and t.right are either null or not null at the same time , then lf is 1 otherwise it is 0 . 如果两个t.leftt.right要么null或不null 的同时 ,然后lf1 ,否则是0

Also you have a typo in there. 另外,您还有错字。 The line should be 该行应该是

int lf = ((t.left==null) == (t.right==null)) ? 1:0;

Notice the == between the two null checks. 注意两个空检查之间的==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM