next up previous contents
Next: Linking with Fortran 77 Up: Operator Associativity Previous: Operator Associativity   Contents

operator«

Beware of the associativity of this operator. What will the following code output?
int i=0;
cout << i << " " << i++ << " " << i << "\n";

The answer is:

> 1 0 0

The reason is that operator<<() operates from right to left even though the output will be produced from left to right. The following code will produce the expected result:

int i=0;
cout << i << " ";
cout << i++ << " ";
cout << i << "\n";

The output being:

> 0 0 1



Michael McNeil Forbes 2006-05-26