Basic usage of C++ references

Links to the original text: https://www.bilibili.com/video/av41559729/?p=94

1. Basic Use of References

Role: Aliasing variables
Syntax: Data Type & Alias = Original Name

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int& b = a;//Once initialized, it can't be changed.
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	b = 20;//The original value will also change after modification.
	cout << "Modified a = " << a << endl;
	cout << "Modified b = " << b << endl;

	system("pause");
	return 0;
}

The results are as follows:

2. Notes for citation

References must be initialized and cannot be changed after initialization

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int c = 30;
	//Int & b; Error, reference must be initialized
	int& b = a;//Once initialized, it can't be changed.
	b = c;//This is an assignment operation, not a change reference
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;

	system("pause");
	return 0;
}

The results are as follows:

3. Reference as Functional Parameter

Function: In function inheritance, parameters can be modified by referencing technology; Advantages: It can simplify pointer modification parameters.

#include<iostream>
using namespace std;

//1. Value transfer
void Swap1(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "swap1 a = " << a << endl;
	cout << "swap1 b = " << b << endl;
}

//2. Address Delivery
void Swap2(int* a, int* b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
	cout << "swap2 a = " << (int)*a << endl;
	cout << "swap2 b = " << (int)*b << endl;
}

//3. Reference Passing
void Swap3(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "swap3 a = " << a << endl;
	cout << "swap3 b = " << b << endl;
}

int main()
{
	//1. Value transfer, parameter does not change parameter
	int a = 10;
	int b = 20;
	Swap1(a, b);
	cout << "pass by value a = " << a << endl;
	cout << "pass by value b = " << b << endl;

	//2. Address Delivery
	int c = 40;
	int d = 50;
	Swap2(&c, &d);//Parametric modification
	cout << "Address delivery c = " << c << endl;
	cout << "Address delivery d = " << d<< endl;

	//3. Reference Passing
	int e = 60;
	int f = 70;
	Swap3(e, f);//Reference passing, parameter will modify the actual parameter
	cout << "Reference passing e = " << e << endl;
	cout << "Reference passing f = " << f << endl;
	//Reference parameters produce the same effect as address passing, and the reference grammar is clearer!

	system("pause");
	return 0;
}

The results are as follows:

4. Reference as function return value

Function: References can exist as return values of functions;

Note: Do not return local variable references;

Usage: Function call as left value;

#include<iostream>
using namespace std;

//Returns a local variable reference
int& test1()
{
	int a = 10;
	return a;
}

//Reference to Static Variables
int& test2()
{
	static int a = 20;//Static variables, stored in the global area, the data on the global area is released after the end of the program.
	return a;
}
int main()
{
	//Local variable reference
	int& ret = test1();
	cout << "ret = " << ret << endl;
	cout << "ret = " << ret << endl;//This time the result is wrong, because a's memory has been released.

	//Reference to Static Variables
	int& ret1 = test2();
	cout << "ret1 = " << ret1 << endl;
	cout << "ret1 = " << ret1 << endl;

	//Static variables
	test2() = 1000;
	cout << "ret1 = " << ret1 << endl;
	cout << "ret1 = " << ret1 << endl;

	//local variable
	test1() = 2000;
	cout << "ret = " << ret << endl;
	cout << "ret = " << ret << endl;

	system("pause");
	return 0;
}

The results are as follows:

5. Essence of Citation

#include<iostream>
using namespace std;

//The essence of a reference is a pointer constant
//The discovery is a reference, converted to int * const ref = &a;
void func(int& ref)
{
	ref = 100;//Ref is a reference, converted to * ref = 100;
}

int main()
{
	int a = 10;
	//Pointer pointing cannot be changed
	int& ref = a;
	ref = 20;//Discovery is a reference, * ref = 20 dereference

	cout << "a = " << a << endl;
	cout << "ref = " << ref << endl;
	func(a);

	system("pause");
	return 0;

}

The results were as follows:

6. Constant Reference

#include<iostream>
using namespace std;

//Reference scenarios, usually used to modify parameters
void showValue(const int& val)
{
	cout << val << endl;
}

int main()
{
	const int& ref = 10;

	//ref = 100; // Add const and change to an unmodifiable value
	cout << ref << endl;

	//Using Constant Reference in Functions to Prevent Misoperation and Modify Arguments
	int a = 10;
	showValue(a);

	system("pause");
	return 0;

}

The results were as follows:10.

Posted on Tue, 08 Oct 2019 20:14:38 -0700 by gunslinger008