(c + +) Lvalue, rvalue issues for i++ and i++

Source: Internet
Author: User

1. What are left and right values?

The left value is the value that appears to the left of the expression (the left side of the equals sign) and can be changed, which is the address of the memory that stores the data value, also known as the variable's address;

The right value refers to the data stored in a memory address, also known as a variable.

The lvalue can be the right value, but the right value cannot be an lvalue.

Therefore only the left value can be taken to address.

2. To raise two questions:

int i = 0;

(i++) +=i; Error

(++i) +=i; That's right

int *ip = & (i++); Error

int *ip = & (++i); That's right

3, why i++ can not be left value?

We see the realization of i++ and i++.

The prefix form:int& int::operator++ ()//is returned here is a reference form, that is, the function return value can also be used as an lvalue to use the {//function itself without parameters, which means to add 1 of the  *this + + 1 in its own space;  Add  return *this;  Retrieve value}
Suffix form: const int int::operator++ (int)//function return value is a non-lvalue type, which differs from the prefix form. {//function with parameter, indicating there is another space to open  int oldValue = *this;  Fetch value  + + (*this);  Add  return oldValue;  Returns the retrieved value}

Simply understood, is that i++ returns a temporary variable that cannot be addressed after the function is returned, it is just a data value, not an address, and therefore cannot be left-valued.

A simpler code explanation:

I++:{int Tmp;tmp=i;i=i+1;return tmp;} ++i:{i=i+1;return i;}

Reference article:

http://blog.csdn.net/zlhy_/article/details/8349300

  

(c + +) Lvalue, rvalue issues for i++ and i++

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.