New features in C ++ 11 (1) Right Value reference

Source: Internet
Author: User

In C ++, the left value (lvalue) is the amount of addresses that can be obtained. Because it often appears on the left of the value assignment statement, it is called the left value. For example, a variable with a name. For example:

Int a = 10; // a is a left value.

Traditional C ++ references are left-value references. For example, int & ra = a; associates ra with. This is the left value reference.

C ++ 11 introduces the concept of right value reference. Use & to represent the reference of the right value.

First, let's look at what is the right value. It can be said that all the values that are not left values are right values. For example, text, temporary object, or temporary value (the amount of addresses cannot be obtained ).

The right value reference is a reference to the right value. In particular, this is a concept used when the right value is a temporary object. For example.

Int & ra = 10;
10 is a right value, and ra will be a reference to the right value of 10. Example:

# Include
 
  
Using namespace std; int main () {int & ra = 9; cout <& ra <
  
   
Output result:
   


Interestingly, if you associate the right value with the right value reference, the right value will be stored in a specific location and the address of the location can be obtained. For example, you cannot perform & Operations on 9 or 5 on the right, but you can perform & Operations on ra.

The purpose of the right value reference is to provide some methods that can be used when a temporary object is involved (mainly copying constructor and operator =, but not limited to this ). Because it is known that the temporary object will be destroyed, some operations that involve copying a large amount of data can be implemented by simply copying pointers pointing to these values.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + t723qL/J0tS9qyZhbXA7JmFtcDvX986qss7K/cu1w/e1xNK7sr + 31qOstNO2 + placement = "brush: java;"> # include Using namespace std; void showMax (int & a, int & B) {if (a> B) cout <发现showmax(20,15)的时候无法正常编译。< p>

This is because 20, 15 is a right value. Next we will define a right value to reference the version of showMax ();

# Include
    
     
Using namespace std; void showMax (int & a, int & B) {if (a> B) cout
     
      
Running result:
      



When showMax (20, 15) is called, the compiler automatically calls the version referenced by the corresponding right value.

When being used as a method parameter, the right value reference is very useful, for example:

# Include
       
        
Using namespace std; void show (int & a) {cout <"Left reference:" <
        
Result:


Note that:

Show (a ++); // reference the right value
Show (++ a); // reference the left Value

A ++ extracts a copy of Persistent Object a first, adds 1 to the value of Persistent Object a, and returns the copy, the copy object is a temporary object (the address cannot be obtained), so it is the right value;

++ A adds 1 to the value of Persistent Object a and returns the permanent object a (which can be an address). Therefore, it is the left value.


The right-value reference is rarely used independently. It is generally used as a parameter of a method or function. The most commonly used is on the mobile semantics.



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.