From what I understand, C++ works from left to right. For example, if I do:
std::cout << "output" << "stream";
C++ first begins at the leftmost thing (std::cout), next there is the << operator which takes the string literal at the right ("output") and put it to the object at the left of the operator (std::cout). C++ then returns the object at the left of the operator (std::cout) and then continues the code, another << operator.
What does the "=" operator return?
If I do
double wage = 5;
double salary = wage = 9999.99;
I thought the "=" operator would return only the left or right operand of the "=". So, with my logic, in the line of the salary initialization, salary is initialized with the value of wage, then the "=" operator returns either salary or wage (let's say salary), then it assign 9999.99 to salary, but wage is gone, it should keep its value of 5.
But when I check the value of "salary" and "wage" after the "salary" initialization, both have a value of 9999.99. If I apply the same logic I used with std::cout above, there should be only one variable, either "salary" or "wage", with a value of 9999.99
=in particular is right-associative; it works right to left. - Igor Tandetnikdouble salary = wage = 9999.99;so the associativity doesn't even matter. - Ben Voigt