###C++ reference a reference variable is an alias, that is, it is another name for an existing variable. Once a reference is initialized to a variable, the reference name or variable name can be used to point to the variable. ####C++ references vs pointer references are easily confused with Pointers, with three major differences:

  • There is no empty reference. References must be connected to a valid block of memory.
  • Once a reference is initialized to an object, it cannot be pointed to another object. A pointer can point to another object at any time.
  • The reference must be initialized at creation time. Pointers can be initialized at any time. ####C++ create a reference imagine that a variable name is a label attached to a variable’s memory location. You can think of the reference as the second label attached to a variable’s memory location. Therefore, you can access the contents of a variable by either the original variable name or a reference.

###### The following is an example:

#include <iostream>

using namespace std;

int main()
{
	int i;
	int& r = i;
	i = 5;
	cout << "Value of i : " << i << endl;
	cout << "Value of i reference : " << r << endl;
	cout << "Addr of i: " << &i << endl;
	cout << "Addr of r: " << &r << endl;

	int x =6;
	int y = x;
	cout << "Value of x : " << x << endl;
	cout << "Value of y : " << y << endl;
	cout << "Addr of x: " << &x << endl;
	cout << "Addr of y: " << &y << endl;

	system("pause");
	return0; } Value of I: 5 Value of I reference: 5 Addr of I: 00EFFA88 Addr of R: 00EFFA88 Value of x: 6 Value of y: 6 Addr of x: 00EFFA70 Addr of y: 00EFFA64Copy the code

####int& r = i; And int r = I; The difference is the allocation of memory, which creates another memory space

Note: references must be initialized when they are declared; they cannot be declared first and then assigned.

#include <iostream>

using namespace std;

int main() { int rats = 10; // Declare a reference, Dan is not initializing int & Rodents; rodents = rats;return 0;
}

Copy the code

###### Run error: Error C2530 “Rodents” : A reference must be initialized

####C++ reference as argument reference as function argument C++ adds reference types as function arguments to expand the function’s ability to pass data. C++ function parameters:

(1) Use variable names as arguments and parameters. The parameter is passed the value of the variable and is passed in one direction. If the value of a parameter changes during the execution of a function, it is not passed back to the argument. Because when a function is called, parameters and arguments are not the same store. // Pass the pointer to the variable as c (2). The parameter is a pointer variable, and the argument is the address of a variable. When the function is called, the parameter (pointer variable) points to the unit of the argument variable. This changes the value of an argument through a parameter pointer. // Same as c (3) c ++ provides references to passing variables. A parameter is a reference variable, and an argument is a variable. When a function is called, the parameter (reference variable) refers to the unit of the argument variable. This reference by parameter changes the value of the argument.

###### The following is an example:

#include <iostream>using namespace std; Void swap(int& x, int& y); intmain() {int a = 100; int b = 200; cout <<"Before switching, the value of A:" << a << endl;
	cout << "Before switching, the value of B:"<< b << endl; /* Call a function to swap values */ swap(a, b); cout <<"After the swap, the value of A is:" << a << endl;
	cout << "After the swap, the value of B:" << b << endl;

	system("pause");
	return0; } void swap(int &x, int &y) {int temp; temp = x; /* save the value of address x */ x = y; /* assign y to x */ y = temp; /* Assign x to y */return; } Output: Before switching, the value of A: 100; before switching, the value of B: 200; after switching, the value of A: 200; after switching, the value of B: 100Copy the code

####C++ makes C++ programs easier to read and maintain by using references instead of Pointers. C++ functions can return a reference in the same way that they return a pointer. (2) The great advantage of returning a function value by reference is that there is no memory copy of the returned value. When a function returns a reference, it returns an implicit pointer to the returned value. This way, the function can be placed to the left of the assignment statement. ###### The following is an example:


#include <iostream>using namespace std; Double vals [] = {10.1, 12.6, 33.1, 24.1, 50.0}; double&setValues(int I) // note & {returnvals[i]; Return a reference to the ith element} // Call the main function int of the function defined abovemain()
{

	cout << "Value before change" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "vals[" << i << "] =";
		cout << vals[i] << endl;
	}

	setValues (1) = 20.23; // Change the second elementsetValues (3) = 70.8; // Change the 4th element cout <<"Changed value" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "vals[" << i << "] =";
		cout << vals[i] << endl;
	}
	system("pause");
	return0; } Result output: Values before change, vals[0] = 10.1 VALS [1] = 12.6 VALS [2] = 33.1 VALS [3] = 24.1 VALS [4] = 50 Values after change, vals[0] = 10.1 VALS [1] = 20.23 Vals [2] = 33.1 VALS [3] = 70.8 VALS [4] = 50Copy the code

When returning a reference, be careful that the referenced object is not out of scope. It is illegal to return a reference to a local variable, but it is possible to return a reference to a static variable. ##### Note incorrect use:

int& func() { int q; / /!returnq; // Error at compile time static int x;returnx; // safe, x is still valid outside function scope}Copy the code

The ##### reference as a return value must follow the following rules:

  • A reference to a local variable cannot be returned. The main reason is that local variables are destroyed when the function returns, so the returned reference becomes a “no-reference” reference and the program goes into an unknown state.
  • Cannot return a reference to memory allocated by new inside the function. While there is no problem with passive destruction of local variables, there are other awkward situations in this case (returning a reference to new allocated memory within a function). For example, if a reference returned by a function appears as a temporary variable without being assigned an actual variable, the space to which the reference points (allocated by new) cannot be freed, resulting in a memory leak.
  • It is possible to return a reference to a class member, but preferably const. The main reason is that when the property of an object is associated with a business rule, the assignment is often related to some other property or the state of the object, so it is necessary to encapsulate the assignment in a business rule. If other objects can obtain an inordinate reference (or pointer) to the property, then a mere assignment of the property would undermine the integrity of the business rule.

Special thanks to:

Rookie C++ tutorial