[英]Conditional logical operator working with =
I know that the following code will execute both the left and right expressions : 我知道以下代码将同时执行左右表达式:
bool myBool = true;
myBool &= Foo();
Is there any way to write the same way (a kind of &&=
) a code that will produce : 有没有办法用相同的方式(一种
&&=
)编写将产生的代码:
bool myBool = true;
myBool = myBool && Foo();
Instead of 代替
myBool = myBool & Foo();
If you want Foo()
be executed, just swap : 如果要执行
Foo()
,只需交换 :
bool myBool = ...;
myBool = Foo() && myBool;
Will this answer your question? 这会回答您的问题吗? (wasn't really clear)
(不是很清楚)
bool myBool = true;
myBool = myBool || (!myBool && Foo());
Only if the myBool
is false
then the function will be executed 仅当
myBool
为false
,函数才会执行
As already mentioned there is no &&=
-operator thus you can´t overload any. 如前所述,没有
&&=
运算符,因此您不能重载任何运算符。 For a list of operators you can overload for any type see here . 有关运算符的列表,您可以重载任何类型的代码,请参见此处 。 For assignement-operators there are no overloads defined at all:
对于赋值运算符,根本没有定义重载:
Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.
赋值运算符不能重载,但是例如+ =可以使用+求值,可以重载。
So your current approach using myBool && Foo();
因此,您当前使用
myBool && Foo();
is the way to go. 是要走的路。
As mentioned on this post the IL for &=
and &
are equal making the former just syntactic sugar of the latter. 如本文所述,
&=
和&
的IL等于使前者只是后者的语法糖。 So there´d be no real value on creating such an operator as all its use would be to get rid of a few characters. 因此,创建这样的运算符没有真正的价值,因为它的全部用途是摆脱一些字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.