Study day 55

Time:2021-1-10

C++

  1. Left and right values

    1. Lvalue can be used on the left side of equal sign in assignment statementRepresents an address
    2. In order to compare the left value, there are right value, opposite to left value
    3. The expression first determines whether it is left valued, non left or right, no other options
    4. Left values can be used as right values
    5. A = a + 1 — A is a left value, not a right value, a is on the right, so a has a right value attribute (non right value attribute), a represents address on the left, a represents value on the right
    6. An lvalue can have both lvalue attribute and lvalue attribute, and the variable is lvalue
    7. We can understand the left value expression as the left value, while the right value expression is the right value
    8. Assignment operator, address operator, container subscript, iterator increment and decrement, etc. all need lvalue. As long as the operator can operate on literal value, lvalue is generally required
    9. The evaluation result of an lvalue expression is an object, it must have an address, and an address is not necessarily an lvalue, but an lvalue must have an address, and the address is the core
  2. Citation classification

    1. There are three ways of quotation
      1. Lvalue reference (bound to lvalue)
      2. Const reference (constant reference) is a kind of lvalue reference, which is characterized by the object whose value cannot be changed
      3. The right value reference (bound to the right value) is equivalent to the left value reference. Its main function is temporary reference
    2. Right value reference use&&A = 1 1 is the temporary value, temporary storage
  3. Lvalue reference

    1. Binding a variable value to an lvalue is an lvalue reference, most of which are lvalue references

    2. A reference must be bound to a value, unlike a pointer with an empty pointer, a reference without an empty reference, initialization binding, and reference must be initialized

    3. An lvalue reference must be bound to an lvalue, not to an lvalue

    4. Int a = 3 // lvalue because changing face is lvalue
        Int & C = A // bind lvalue  
       Int & A1 = + + B // lvalue
    5. The formal parameters in any function are left valued

  4. Right value reference

    1. The reference purpose of the right value reference

      1. Bind to some objects that will be destroyed or temporary
      2. Right value reference & represents a new data type
      3. The purpose is to change the copied object into a moving objectImprove operation efficiency
      4. The moving object is to replace the copy object and move a to B. the way is that a opens up its own memory and B needs to open up memory to store data. The moving object is that B no longer opens up content but directly transfers a’s memory to B, transfers a’s memory directly to B, or B steals all a’s memory
      5. After moving the object, a can no longer be used
      6. Right valued references are used to deal with move constructors and move assignment operators
    2. Must be a reference bound to the right value

    3. Right value reference is also a reference, that is, the name of the object

    4. Right valued references also have addresses

    5. A left value cannot be bound to a temporary variable. A temporary variable has an address, but the system treats it as a right value

    6. If you want to bind a temporary variable to an lvalue, you can use const reference. Const reference can not only bind, but also perform implicit conversion and put the value into a temporary variable

    7. int e;
      	int a = e;
      	int e = 10;
      
      		int& c = a;
      
      
      		Int & & D = 100; // right value
  5. STD:: move function

    1. Casts a left value to a right valueThe only role

    2. No,The function of mobile

    3. int a = 29;
      	Int & & B = STD:: move (a); // forced binding is also the only function of move
    4. Bind B and a together, change B, a will change, change a, B will change

    5. int a = 29;
      	Int & & B = STD:: move (a); // forced binding is also the only function of move
      
      
      	String c = "this is a string"; // after running, the content of C is empty
      	const char* f = c.c_str();
      	String e = STD:: move (c); // in fact, STD:: move triggers the function in string, empties the content in C, string is the move constructor, moves the value of C to e, and
      	const char* s = e.c_str();
      	cout << c << endl;
      	cout << e << endl;
      	cout << b << endl;
    6. Transfer is not done by move, e is the re opened memory to store the contents of C

    7. Both C and E have their own memory

    8. String & & E = STD:: move (c) so that the internal function of string will not be triggered, but the right value reference

  6. Summary

    1. Left value can be bound to left value, right value can be bound to right value, const can be bound to right value or left value

    2. A – – (post increment or decrement operator) returns the right value, – A (pre increment or decrement operator) returns the left value

    3. int a = 1;
      	Int & & B = a + +; // there is no relationship between right value, B and a
      	Int & C = + + A; // lvalue
    4. The system does not recommend to continue to use the moved value, that is, C, after moving, but it can still be used later

    5. int a = 29;
      	Int & & B = STD:: move (a); // forced binding is also the only function of move
      
      
      	String c = "this is a string"; // after running, the content of C is empty
      	const char* f = c.c_str();
      	String & & E = STD:: move (c); // the system does not allow C to appear in future code
      	const char* s = e.c_str();
      	C = ABC "; // it can still be used later
      	//e = "aaa";
      
      	cout << c << endl;
      	cout << e << endl;
      	cout << b << endl;

Recommended Today

020_CSS3

catalog How to learn CSS What is CSS History of development quick get start Advantages of CSS Three ways to import CSS Expansion: two ways of writing external style selector Basic selector Hierarchy selector Structure pseudo class selector attribute selectors Beautify web page elements Why beautify web pages Span label: for the text that needs […]