4.4. Assignment Operators

The left-hand operand of an assignment operator must be a modifiable lvalue. For example, given

int i = 0, j = 0, k = 0; // initializations, not assignmentconst int ci = i;        // initialization, not assignment

Each of these assignments is illegal:

1024 = k;      // error: literals are rvaluesi + j = k;     // error: arithmetic expressions are rvaluesci = k;        // error: ci is a const (nonmodifiable) lvalue

The result of an assignment is its left-hand operand, which is an lvalue. The type of the result is the type of the left-hand operand. If the types of the left and right operands differ, the right-hand operand is converted to the type of the left:

k = 0;          //  result: type int, value 0k = 3.14159;    //   ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.