C + + rvalue reference

Source: Internet
Author: User

from:51058236

This article is for your own use only

1. The concept of lvalue and rvalue values

An lvalue is a value that can be assigned to the left of the assignment number, and the left value must have an entity in memory ;
Right worth the value assigned to the other variable on the right of the assignment, and the right value can be in memory or in the CPU register .
when an object is used as an rvalue, its content (value) is used, and its address is used when it is treated as a left-hand value.

2. References

References are optimizations made by C + + syntax, and the nature of references is achieved by pointers . refers to an alias equivalent to a variable .

A reference can change the pointer's direction, and it can also change the value that the pointer points to.

Basic rules for referencing:

    1. When declaring a reference, it must be initialized, and once bound, the reference must not be bound to another object, i.e. the reference has to be initialized and The reference cannot be redefined;
    2. All operations on the reference are equivalent to the operation of the original object.

3. Lvalue Reference and Rvalue reference

3.1 Lvalue Reference
The basic syntax for Lvalue references: type & reference name = Lvalue expression;

3.2 Rvalue Reference

The basic syntax for rvalue references type && reference name = Rvalue expression;

Rvalue references are often used by enterprise developers in code optimization.

There can be no spaces in the middle of the "&&" of the rvalue reference.

1#include <iostream>2 using namespacestd; 3   4 intMain ()5 {  6cout <<"-------Reference Lvalue--------"<<Endl; 7     intA =5; 8     int&add_a (a); 9   Tencout <<"A ="<< a <<"    "<<"&a ="<<&a<<Endl;  Onecout <<"add_a ="<< add_a<<"    "<<"&add_a ="<< &add_a <<Endl;  Acout <<"-----------------------"<<Endl;  -    -cout <<"-------Reference rvalue--------"<<Endl;  the     intb =Ten;  -     int&&add_b (b +1);  -cout <<"B ="<< b <<"    "<<"&b ="<< &b <<Endl;  -cout <<"add_b ="<< add_b <<"    "<<"&add_b ="<< &add_b <<Endl;  +add_b++;  -cout <<"add_b++ ="<< add_b <<"    "<<"&add_b++ ="<< &add_b <<Endl;  +cout <<"-----------------------"<<Endl;  A    atSystem"Pause");  -     return 0;  -}

Operation Result:

In fact, for the lvalue is still very good understanding, mainly for the right value is not good understanding, especially the code of 16 lines: the right value of the example. C + + is designed to reference the syntax of rvalue, mainly because for similar b+1, such an operation occurs on the CPU register, it can not take address, assignment, etc., so this type of operation can only be placed on the right side of the equal sign, assign it to other variables. If the equal sign appears to the right: &b, this is also the right value, because the action to take the address symbol is also done in the register. Therefore, it cannot be used as the left value.

Study notes:

Left and right values

int NUM1 (5);
int num2 (10);
int *pnum (&NUM1);//&num1 in register
int * & rpnum = pnum;//reference Lvalue
Rpnum = &num2;//Reference can change pointer pointing
*rpnum = 100;//Reference can also change the value of the pointer
int * && rpnum = &num1;//reference rvalue,&& Middle cannot have spaces C + + syntax used in memory optimization in enterprise development is often used in the CPU

int *p = rpnum;//for object copy

Changing an external variable requires an address or a reference
Lvalue references are implemented with pointers.

C + + rvalue reference

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.