lvalue and rvalue for pre/postfix increment

后端 未结 1 730
不思量自难忘°
不思量自难忘° 2021-01-18 07:44

Learning the lvalue and rvalue. The definition is whatever that can be \"address of\" is the left value and otherwise, it is rvalue.

I checked the operator precedenc

相关标签:
1条回答
  • 2021-01-18 08:06

    Based on my comment you can see why the compiler is against this kind of operation. The problem lies in general implementation of postfix operator ++, which copies the object (int in this case), increments the original one and returns that preincremented copy. You can think of it like of a function that is defined in following way:

    int foo_operator(int& a)
    {
        int copy = a;
        a += 1;
        return copy;
    }
    

    If you try to use that function in your example, your compiler will also protest against it. The return value of that function is an rvalue.

    You might now ask - what's up with the prefix operator ++? Isn't that also a function that returns a value? And the answer would be - no. Prefix operator ++ returns a reference, not a copied value, thus the 'outcome' of it can be used with operand &.

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