| =运算符在C ++中意味着什么? [英] What does the |= operator mean in C++?

查看:183
本文介绍了| =运算符在C ++中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

| =运算符在C ++中意味着什么?

What does the |= operator mean in C++?

推荐答案

假设您在整数上使用内置运算符, slyly重载的用户定义类的操作符,这些是相同的:

Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:

a = a | b;
a |= b;

' | = 按位或赋值运算符。它计算RHS('b')与LHS('a')的OR'的值,并将结果赋给'a',但它在这样做时只评估'a'。

The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.

'| ='运算符的最大优点是'a'本身是一个复杂的表达式:

The big advantage of the '|=' operator is when 'a' is itself a complex expression:

something[i].array[j]->bitfield |= 23;

vs:

something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;

这是有意的还是意外的?

Was that difference intentional or accidental?

...

答案:故意 - 显示速记表达式的优势...第一个复杂表达式实际上等效于:

Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:

something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;






类似的注释适用于所有复合赋值运算符:


Similar comments apply to all of the compound assignment operators:

+= -= *= /= %=
&= |= ^=
<<= >>=

任何复合运算符表达式:

Any compound operator expression:

a XX= b

等于:

a = (a) XX (b);

请注意这里的括号 - 它显示了分组的工作原理。

except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.

这篇关于| =运算符在C ++中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆