Assignment operators
The result of an assignment expression is not an lvalue.
The result of an assignment expression is an lvalue.
All assignment operators have the same precedence and have right-to-left associativity.
Simple assignment operator =
The simple assignment operator has the following form:
lvalue =
expr
The operator stores the value of the right operand expr in the object designated by the left operand lvalue.
If the
left operand is not a class type, the right operand is implicitly
converted to the type of the left operand. This converted type is
not be qualified by const
or volatile
.
If the left operand is a class type, that type must be complete. The copy assignment operator of the left operand is called.
If the left operand is an object of reference type, the compiler assigns the value of the right operand to the object denoted by the reference.
A packed structure or union can be assigned to a nonpacked structure or union of the same type. A nonpacked structure or union can be assigned to a packed structure or union of the same type.
If one operand is packed and the other is not, z/OS® XL C/C++ remaps the layout of the right operand to match the layout of the left. This remapping of structures might degrade performance. For efficiency, when you perform assignment operations with structures or unions, you should ensure that both operands are either packed or nonpacked.
Compound assignment operators
The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.
The following table shows the operand types of compound assignment expressions:
Operator | Left operand | Right operand |
---|---|---|
+= or -= | Arithmetic | Arithmetic |
+= or -= | Pointer | Integral type |
*=, /=, and %= | Arithmetic | Arithmetic |
<<=, >>=, &=, ‸=, and |= | Integral type | Integral type |
a *= b + c
is
equivalent to a = a * (b + c)
and not a = a * b + c
The following table lists the compound assignment operators and shows an expression using each operator:
Operator | Example | Equivalent expression |
---|---|---|
+= | index += 2 |
index = index + 2 |
-= | *pointer -= 1 |
*pointer = *pointer - 1 |
*= | bonus *= increase |
bonus = bonus * increase |
/= | time /= hours |
time = time / hours |
%= | allowance %= 1000 |
allowance = allowance % 1000 |
<<= | result <<= num |
result = result << num |
>>= | form >>= 1 |
form = form >> 1 |
&= | mask &= 2 |
mask = mask & 2 |
‸= | test ‸= pre_test |
test = test ‸ pre_test |
|= | flag |= ON |
flag = flag | ON |
Although the equivalent expression column shows the left operands (from the example column) twice, it is in effect evaluated only once.
In addition to the table of operand types, an expression is implicitly converted to the cv-unqualified type of the left operand if it is not of class type. However, if the left operand is of class type, the class becomes complete, and assignment to objects of the class behaves as a copy assignment operation. Compound expressions and conditional expressions are lvalues in C++, which allows them to be a left operand in a compound assignment expression.