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

前端 未结 4 1829
故里飘歌
故里飘歌 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:10

    For built-in types in C++ evaluating an assignment expression produces an lvalue that is the left hand side of the assignment expression. The assignment is sequenced before the result can be used, so when the result is converted to an rvalue you get the newly assigned value:

    int a, b=5;
    int &c = (a=b);
    assert(&c==&a);
    b=10;
    assert(10==(a=b));
    

    C is almost but not exactly the same. The result of an assignment expression in C is an rvalue the same as the value newly assigned to the left hand side of the assignment.

    int *c = &(a=b); // not legal in C because you can only take the address of lvalues.
    

    Usually if the result of an assignment is used at all it's used as an rvalue (e.g., a=b=c), so this difference between C++ and C largely goes unnoticed.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 23:23

    The assignment operator is defined (in C) as returning the value of the variable that was assigned to - i.e. the value of the expression (a=b) is the value of a after the expression has been evaluated.

    It can be defined to be something different (of the same type) for user-defined operator overloads in C++, but I suspect most would consider this to be a very unpleasant use of operator overloading.

    You can use this (non-boolean) value in a while (or an if, etc.) because of type conversion - using a value in a conditional context causes it to be implicitly converted to something which makes sense in a conditional context. In C++, this is bool, and you can define your own conversion (for your own type) by overloading operator bool(). In C, anything other than 0 is true.

    0 讨论(0)
  • 2020-12-16 23:24

    To understand such expressions, you have to first understand that, positive integers are considered as 'true' and 0 is considered as false.

    An assignment evaluates to the left hand side of the operator = as its value. So, while(a=b) { } would mean, while(1 /*true*/) if a after being assigned to b evaluates to non-zero. Else, it is considered as while(0 /*false*/)

    Similarly, with the operator (a=b)?1:0 is the value of a after being assigned to b .. if it is non-zero then the value is taken as true and the statement following ? will be executed, or the statement following : is executed.

    Assignments usually evaluate to the value at the left hand side of the operator = where as, logical operators(such as ==, && etc) evaluate to 1 or 0.

    Note: with C++, it will depend upon if or not, a certain operator is overloaded.. and it will also depend upon the return type of the overloaded operator.

    0 讨论(0)
提交回复
热议问题