This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging
Reference is divided into lvalue reference and Rvalue reference. This paper mainly discusses lvalue reference
Let’s start with the definition of a quote:
A reference gives an object another name, and a reference type refers to another type. Write the reference type with the & specifier.
The definition is obscure, but let’s go straight to the example:
int x=1024;
int &y=x;
std::cout<<y<<std::end;
Copy the code
The sample output is 1024;
The above example is easy to understand, and the details of the reference are worth pondering
Details:
A reference is an alias. A reference is not an object. Instead, it is just another name given to an existing object.
Details of the two:
References must be initialized, and once the object is bound, it cannot be rebound!! It’s simply monogamy or monogamy, but it can’t be called monogamy because a variable can have multiple references.
Example:
int a=1;
int &b=a;// If b is bound to a, b intelligence can only represent A;
int a1=2;
b=a1;//b=2; a=2;
int &c=b;// B and C stand for A
Copy the code
After b is bound to A, B can only represent A, and b and C both represent A.
Details of the three:
Variable applications cannot bind to constants, but constant references can!!
Example:
const int &x=1234;/ / right
//int &x=1234; / / error
cout<<x<<endl;
Copy the code
Details of the four:
References make it look like they have the same size and address as the original variable!!
1, sizeof (a) = = sizeof (b);
2, & a = = & b;
Both are false, false. Only with sizeof() and &, the compiler returns the size and address of the original object, because b is an alias of a, and C++ makes b the same as a, so it’s called an alias.
Details of the five:
The referenced layer is implemented with a pointer, whose size is the size of the corresponding type pointer!
A variable of a reference type consumes the same amount of memory as a pointer type.
Details of the 6:
Reference types do not count as function overloads! Because they have the same signature!
Example:
void AA(int x){}
void AA(int &x){}
Copy the code
Function overloading does not occur in the above example! But the ones below do
void AA(int x){}
void AA(int x)const{}
Copy the code
The above examples are all in the case of member functions, because a const trailing function can only occur in a member function;
When should you use a quote?
1. When parameters are passed
Parameter passing value passing vs reference passing, value passing requires extra stack memory, reference only needs to open up the size of the stack memory. Try to pass the parameter reference, reference is equivalent to another name of the variable, of course, the value of passing also has useful places, such as this value change will affect other content, if it does not affect and does not change the value of the variable as far as possible reference, such as backtracking algorithm in the recursion, the final result with reference, some of the middle variables with value pass
2. Reference to the return value
Whether the result of the operation of the function is placed in an existing memory location or a newly created memory location. If the result of a function operation is placed in the local position of the function, the end local of the function is released, and no pointer or reference is allowed.
The end of this article, I believe you harvest full!