Explain left and right associativity to me

Hello,

when i first was trying to learn c++ i associated cout with <<,

the << i envisioned going outward, so that it was printed on the screen. I did the inverse with cin so that it brings in something to store, like a name.

I believe now that may be a horrible association, and that i missed the point of what it really means.

so now im struggling to understand how left and right associativity actually works. does it mean that << is read left to right? and the inverse is true of >>?

what i mean, is the entire line read left to right/ right to left?
The associativity of operator>> and operator<< is left-to-right.

That is: std::cout << a << b and std::cin >> a >> b are equivalent to ((std::cout << a) << b) and ((std::cin >> a) >> b), respectively.

One might want to contrast that with operator= which has right-to-left associativity.

a = b = 6 is equivalent to (a = (b = 6))
Associativity has nothing to do with stream operators.
The operators are used just to represent the flow of data, and the standard committee decided that they wanted the stream object to be always on the left of the expression.
If you wanted, you could write code to do var >> cout; var << cin and the associativity would still be the same.

Edit:
Actually now that I think about it, the stream is always on the left probably BECAUSE the shift operators are left-associative.
Last edited on
The operators just describe the flow of data.

std::cout << "Hola, amigo!";

The operator just describes the way the string is going to the cout object to be printed to the screen.

 
std:cin >> bobby;


The operator just describes the flow of data from the user input to the variable.
I asked because i was thinking that there were other scenarios that the stream operators would trip me up,

is cout and cin just used for a command prompt output/input? If you were coding something with GUI would you still be using cout and cin?

I guess i was close with how i was looking at the stream operators.

but i'm having a little trouble with the associativity in Cire's examples. I can't remember how to even do long division, never passed an algebra class. It seems like there is alot of interplay between math and c++ language.

on the associativity, i'm now wondering why one would be left and another right? Is that down to the context, for example, using division or addition? Just wondering how i would determine what gets equated first.
is cout and cin just used for a command prompt output/input? If you were coding something with GUI would you still be using cout and cin?
AFAIK yes. In GUI you'd use them to log info about the program.
For example, in linux sometimes graphical apps don't start. To find out why I start them from a terminal so that I can read the errors logged to cout/cerr (assuming the createor logs them, it's not obligatory).

It seems like there is alot of interplay between math and c++ language.
I don't think there's much, but that also depends on your field of work. Game development is one of the most popular where there is a lot.

Just wondering how i would determine what gets equated first.
It's written in the language standard, but reading that drives you insane. You're better off looking for a table on google. Also any respectable beginner book should have one.
Topic archived. No new replies allowed.