Low level details of C/C++ assignment operator implementation. What does it return?

前端 未结 4 1828
故里飘歌
故里飘歌 2020-12-16 22:36

I m a total newbie to a C++ world (and C too). And don\'t know all its details. But one thing really bothers me. It is constructions like : while (a=b) {...} .

4条回答
  •  醉梦人生
    2020-12-16 23:11

    The assignment operators in C and C++ return the value of the variable being assigned to, i.e., their left operand. In your example of a = b, the value of this entire expression is the value that is assigned to a (which is the value of b converted into the type of a).

    So you can say that the assignment operator "returns" the value of its left operand.

    In C++ it's a little more complicated because you can overload the = operator with an actual user-defined function, and have it return something other than the value (and type) of the left operand.

提交回复
热议问题