An lvalue is an expression that identifies a non-temporary object. An rvalue is an expression that identifies a temporary object or is a value (such as a literal constant) not associated with any object. As a general rule, if you have a name for a variable, it is an lvalue, regardless of wherther it is modifiable.
In C++11, we can have two types of references: lvalue references and rvalue references.
In c++11, an lvalue reference is declared by placing an & after some type. An lvalue reference then becomes a synonym(i.e.,another name)for the object it references.
In C++11,an rvalue reference is declared by placing an && after some type. An rvalue reference has the same characteristics as an lvalue reference except that. inlike an lvalue reference, an rvalue reference can also reference an rvalue(i.e.. a temporary).
lvalue references use
-
aliasing complicated names.///copy. auto & whichList = theLists[ myhash( x , theLists.size())];
-
range for loops.for(auto & x: arr) ++x;
-
avoiding a copy.///auto &x=findMax(arr);///if auto is not used. the typically a non-modifiable reference is explicitly stated with const;
This code illustrates two important concepts:
-
Reference variables are often used to avoid copying objects avoid copying objects across function-call boundaries(either in the function call or the function return).
-
Syntax is needed in function declarations and returns to enable the passing and returning using references instead of copies.
-
我自己写的小网站www.caozhicong.com