C + + supplements--Reference
Preface
A reference is an alias. An alias is another name for an existing object. Its own meaning is not difficult to understand, but with other concepts of a combination, it becomes the use of difficulties. Coupled with the new standard proposed a new reference-rvalue reference, reference to this concept becomes more difficult to understand and use.
Body
With the new standard, which is often the new technology, the concept of reference is divided into two categories: Lvalue reference and Rvalue reference. Where Lvalue references are the most common, and rvalue references are an innovation in language usage.
1. Lvalue Reference
Basic syntax for lvalue references
Type & Reference name = Lvalue expression;
#include <iostream>using namespace Std;int main () {int a = 10;//ra is a reference (alias), which is equivalent to binding RA to a. int &ra (a); cout << ra << " << (void*) &ra <<" "<< (void*) &a << Endl;cin.get (); return 0;}
Run
Basic rules for referencing
- When declaring a reference, it must be initialized, and once bound, the reference cannot be bound to another object.
- All operations on the reference are equivalent to the operation of the original object.
2. Rvalue reference
These are the most common ways of referencing, which we call Lvalue references. In order to distinguish the concept of left and right value references, we first look at the concept of left and right values.
First, the left and right values are the properties of an expression. What is an expression? An expression consists of one or more operand objects. Literals and variables are the simplest expressions.
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.
int a = 1;
A + 1; A + 1 is an expression
In the old C + + standard, we can refer to a, but we can't refer to a+1, which is why?
This is because when variable a is created, it is always present within its scope. As a result, its references are meaningful. While A+1 is not an object, it is temporarily present in the register in the operation, and the value in the register is always refreshed, and creating a a+1 reference does not make any sense. In order to successfully use a reference similar to a+1, this right value. The new C + + standard presents a reference-rvalue reference that differs from the previous.
Basic syntax for rvalue references
Type && Reference name = right value expression;
#include <iostream>using namespace Std;int main () {int a = 10;//rvalue reference int &&ra (a+1); cout << RA << E NDS << (void*) &ra << ends << (void*) &a << endl;ra++;cout << ra << endl;cin.get (); return 0;}
Run
A possible internal implementation interpretation of rvalue references
int a = 1;
int *p = new Int (a + 1);
int &ra = *p;
Delete p; No longer using references, the release of dynamic memory is automatically managed by C + +
The second to third two sentences correspond to rvalue references: int &&ra (A+1);
3. References as function arguments
When referencing as a function parameter, it has the same function as a pointer.
#include <iostream>using namespace std;void fun (int &ra) {cout << ra << ends << (void*) &ra << endl;ra++;} int main () {int a = 10;fun (a); cout << a << ends << (void*) &a << endl;cin.get (); return 0;}
Run
Within the function fun, a change is made to the value of the parameter RA, which affects the argument a. Obviously the use of references is simpler than pointers.
4. References and Arrays
int A[]{1, 2, 3, 4, 5};
int (&RA) [5] = A; Referencing an array
5. References and pointers
Referencing a first-level pointer
int a (0), *p = &a;
int *&RP = p;
Reference level Two pointers
int a (0), *p = &a;
int **pp = &p;
int **&RP = PP; Refer to Level Two pointers, if compilation does not pass, use INT (* * (&RP)) = PP;
6. Reference and Function pointers
#include <iostream>using namespace std;int Add (int a, int b) {return a + B;} int sub (int a, int b) {return a-A;} int mul (int a, int b) {return a * b;} int divi (int a, int b) {if (b) return A/b;elsereturn INFINITY;} int main () {int a (+), B (ten); cout << "Use function name" << endl;cout << Add (A, b) << ends << Sub (A, b) << ends << Mul (A, b) << ends << divi (A, b) << Endl;//padd is a function pointer int (*pfun) (int, int); cout << "Using function pointer" << endl;pfun = add;cout << Pfun (A, b) << ends;pfun = Sub;cout << Pfun (A, B) << ends;pfun = mul;cout << Pfun (A, b) << ends;pfun = Divi;cout << Pfun (A, b) << endl;//function pointer array int (*pfuns[]) (int, int) {Add, Sub, mul, divi};cout << use array of function pointers << endl;cout << Pfuns[0] (A, b) << ; Ends << Pfuns[1] (A, b) << ends << pfuns[2] (A, b) << ends << Pfuns[3] (A, b) << Endl;cou T << reference to function pointer << endl;int (* (&rfun0)) (int, int) (pfuns[0]), Int (* (&RFUN1)) (int, int) (pfuns[1]), Int (* (&RFUN2)) (int, int) (pfuns[2]), Int (* (&RFUN3)) ( int, int) (pfuns[3]); cout << Rfun0 (A, b) << ends << rfun1 (A, b) << ends << rfun2 (A, b) <&l T Ends << Rfun3 (A, B) << endl;cin.get (); return 0;}
Run
Directory of this column
- C + + Supplements directory
Directory of all content
C + + supplements--Reference