C + + Lvalue right-value

Source: Internet
Author: User

recently in the study of C + + Lvalue right value, handling, collecting some other people's information for their own records and learning, if later saw a better explanation, will continue to add. (hit "? "Is the place I don't understand) reference: "Boost Library quest--depth analysis C + + quasi-standard library (2nd edition)" 9787302342731 lvalue: A variable that can be used to store data, with the actual memory address (variable name); rvalue (non-lvalue): the "temporary" variable of "anonymous", which terminates at the end of an expression, cannot hold data, can be modified, or is not modified (const-decorated). discriminant lvalue Right value: The Lvalue can take the address operator & get the address, the right value does not, a compilation error occurs.  The result of all C + + expressions does not know that the lvalue is the right value(? )
++i is an lvalue, i++ rvalue (returns a temporary variable that cannot be assigned);(? )You can transfer right-worthy content to other objects, which completely eliminates the expensive copy cost of the previous, and t& Lvalue refers to the;t&& rvalue reference (C++11). C + + Primer 3rd only use the post operator (i++) when necessary, the predecessor operator returns an Lvalue (the object itself), and the latter operator returns an rvalue. The predecessor operator requires less work, only adds 1 to return the result after adding 1, and the latter operator must first save the original value in order to return the values that are not added to 1 o'clock, which can take a greater price. So suggest ++i。 MSDNLvalues and Rvalues Visual Studioversion

Each C + + expression is either an lvalue or a right-hand value. An Lvalue is an object that is persisted outside a single expression . you can treat an lvalue as an object with a name. all variables, including (const) variables that cannot be changed, arelvalue values. An lvalue is a temporary value that is not reserved outside the expression that uses it. to better understand the difference between left and right values, consider the following example:

lvalues_and_rvalues1.cpp//compile with:/ehsc#include <iostream>using namespace Std;int main () {   int x = 3 + 4;   cout << x << Endl;}

In this example,x is an lvalue because it is reserved outside the expression that defines it. The expression 3 + 4 is a right value because it evaluates to a temporary value that is not reserved outside the expression that defines it .

The following example shows several correct and incorrect uses of the left and right values:

Lvalues_and_rvalues2.cppint Main () {   int i, J, *p;   Correct usage:the variable I was an lvalue.   i = 7;   Incorrect usage:the left operand must is an lvalue (C2106).   7 = i; C2106   J * 4 = 7;//C2106   //Correct usage:the dereferenced pointer is an lvalue.   *p = i;    const int CI = 7;   Incorrect usage:the variable is a non-modifiable lvalue (C3892).   CI = 9; C3892   //Correct usage:the conditional operator returns an lvalue.   ((I < 3)? i:j) = 7;}
Description

The examples in this topic illustrate the correct and incorrect use of operators when they are not overloaded. you can make an expression (such as J * 4) an lvalue by overloading the operator.

When referencing an object reference, the term "lvalue" and "rvalue" are often used. For more information about references, see Lvalue reference declarator:& and rule reference declarator:&&.


pasted from: >  


C + + Lvalue right-value

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.