Assignment Is Right Associative

Unlike the other binary operators, assignment is right associative:

int ival, jval;ival = jval = 0; // ok: each assigned 0

Because assignment is right associative, the right-most assignment, jval = 0, is the right-hand operand of the left-most assignment operator. Because assignment returns its left-hand operand, the result of the right-most assignment (i.e., jval) is assigned to ival.

Each object in a multiple assignment must have the same type as its right-hand neighbor or a type to which that neighbor can be converted (§ 4.11, p. 159):

int ival, *pval; // ival is an int; pval is a pointer to intival = pval = 0; // error: cannot assign the value of a pointer to an intstring s1, s2;s1 = s2 = "OK";  // string literal ...

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.