October 2011
Beginner to intermediate
1200 pages
35h 33m
English
Listing 5.5 uses the following expression to update a loop counter:
i = i + by
C++ has a combined addition and assignment operator that accomplishes the same result more concisely:
i += by
The += operator adds the values of its two operands and assigns the result to the operand on the left. This implies that the left operand must be something to which you can assign a value, such as a variable, an array element, a structure member, or data you identify by dereferencing a pointer:
int k = 5;k += 3; // ok, k set to 8int *pa = new int[10]; // pa points to pa[0]pa[4] = 12;pa[4] += 6; // ok, pa[4] set to 18*(pa + 4) += 7; // ok, pa[4] set to 25pa += 2; // ok, ...
Read now
Unlock full access