In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is left-value reference and right-value reference in C language". In daily operation, I believe that many people have doubts about what is left-value reference and right-value reference in C language. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "what is left-value reference and right-value reference in C language". Next, please follow the editor to study!
Left value, right value
Concept 1:
Left value: what can be put to the left of the equal sign is called the left value.
Right value: something that cannot be put to the left of the equal sign is called the right value.
Concept 2:
Left value: something with an address and a name is the left value.
Right value: something without a name that cannot take an address is the right value.
For example:
Int a = b + c
An is the left value, there is a variable name, you can take the address, or you can put it to the left of the equal sign. The return value of the expression broomc is the right value, has no name and cannot take the address. & (biconc) cannot be compiled and cannot be placed to the left of the equal sign.
Int a = 4; / / an is the left value, and 4 is the right value as the normal literal quantity.
The left values are generally:
Function name and variable name
Returns the function call referenced by the left value
Pre-self-increasing and self-subtracting expressions + + I,-- I
Expressions connected by assignment expressions or assignment operators (aquib, a + = b, etc.)
Dereference expression * p
String literal "abcd"
Pure right value, dying value
Both pure right values and dying values belong to right values.
Pure right value
Temporary variables generated by operational expressions, original literals not associated with objects, temporary variables returned by non-references, lambda expressions, and so on are all pure right values.
For example:
Literal value except string literal value
Returns function calls of non-reference types
Post self-increment and self-subtraction expressions iMel +, iMel-
Arithmetic expressions (axib, axioub, etc.)
& take address expressions, etc.
Dead value
The dead value refers to the new expression related to the right value reference, which is usually the object to be moved, the return value of the std::move function, the return value of the std::move function, and the return value of the converted & type conversion function. The dead value can be understood as the value to be destroyed, and the value obtained by "stealing" the memory space of other variables is used to ensure that other variables are no longer used or are about to be destroyed. It can avoid the release and allocation of memory space, prolong the life cycle of variable values, and are often used to complete special tasks of mobile construction or mobile assignment.
For example:
Class A {xxx;}; An a; auto c = std::move (a); / c is the dead value auto d = static_cast (a); / / d is the dead value
Left value reference, right value reference
You can guess the meaning from the name. The left value reference is the type that references the left value, and the right value reference is the type that references the right value. They are all references, they are all aliases of objects, and they do not own the heap of bound objects, so they must be initialized immediately.
Type & name = exp; / / left value reference type & & name = exp; / / right value reference
Left value reference
Look at the code:
Int a = 5; int & b = a; / b is the left value reference b = 4; int & c = 10; / / error,10 cannot take the address and cannot reference const int & d = 10; / / ok. Because it is a frequent reference and refers to a constant number, the constant number is stored in memory and the address can be taken.
It can be concluded that for the left value reference, the value to the right of the equal sign must be able to take the address, if the address cannot be taken, the compilation will fail, or you can use the const reference form, but the output can only be read through the reference, and the array cannot be modified because it is a constant reference.
Right value reference
If you use a right value reference, the value to the right of the equal sign of the expression requires the right value, and you can use the std::move function to force the left value to be converted to the right value.
Int a = 4; int & & b = a; / / error, an is the left value int & & c = std::move (a); / / ok
Mobile semantics
Before talking about mobile semantics, we first need to understand the concepts of deep copy and shallow copy.
Deep copy, shallow copy
Just take the code as an example:
Class A {public: a (int size): size_ (size) {data_ = new int [size];} A () {} A (const A & a) {size_ = a.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.