C++ references, references as function parameters

Reference concept

References used in C++ are as important as pointers in C language, but they are more convenient and easy to use than pointers , and sometimes even indispensable.

Like pointers, references can reduce data copying and improve data transfer efficiency.

Function parameter passing is essentially a process of assignment (=), and assignment is to copy the memory . The so-called memory copy refers to copying data from one memory to another memory.

The reference can be regarded as an alias of the data, and the data can be found through this alias and the original name .
Reference is similar to shortcuts in Windows. An executable program can have multiple shortcuts, and the program can be run through these shortcuts and the executable program itself.

Reference definition

The definition of references is similar to pointers, except that & replaces *. The syntax format is:

int a = 1int& ra = a;//单独一个a,不需在a的前面加&
//ra 是一个初始化为a的整型引用

References are similar to pointer constants . Once the reference is associated with a variable, the reference will always point to the variable.

int a = 1;
int* const ra = &a;

Reference as variable

The reference must be initialized at the same time as it is defined , and it must be completed in the future, and no other data can be referenced. This is a bit similar to a constant (const variable)

#include <iostream>
using namespace std;
int main() 
{
    
    
    int a = 99;
    int& r = a;
    cout << a << ", " << r << endl;
    cout << &a << ", " << &r << endl;
    return 0;
}
99, 99
0x28ff44, 0x28ff44

In this example, the variable r is a reference to the variable a, and they are used to refer to the same piece of data; it can also be said that the variable r is another name for the variable a.

It can be seen from the output that the addresses of a and r are the same, both are 0x28ff44; or the memory at address 0x28ff44 has two names , a and r. When you want to access the data in the memory, you can use either name.

Note that the reference needs to be added when it is defined, and it cannot be added when it is used. When it is used, adding & means to take the address.
As shown in the code above, the & in the 6th line means a reference, and the & in the 8th line means an address. In addition to these two usages, & can also represent the AND operation in bitwise operations.

Since both the reference r and the original variable a point to the same address, the data stored in the original variable can also be modified by reference

#include <iostream>
using namespace std;

int main() 
{
    
    
    int a = 99;
    int &r = a;
    r = 47;
    cout << a << ", " << r << endl;//47, 47

    return 0;
}

Frequent references
If you do not want to modify the original data by reference , you can add const restrictions when defining, in the form:

int a = 1;
const int& r = a;
int const & r = a;//两种方法都可以

Reference as function parameter

When defining or declaring a function, we can specify the formal parameters of the function as a reference , so that when the function is called, the actual parameters and the formal parameters will be bound together so that they all refer to the same piece of data.

Passing by reference is more intuitive than pointers in terms of use. In the future C++ programming, I encourage readers to use references a lot , it can generally replace pointers (of course pointers are also indispensable in C++), the C++ standard library does the same

#include <iostream>

using namespace std;

void swap1(int* p1, int* p2);
void swap2(int& r1, int& r2);
void swap3(int a, int b);

int main() {
    
    
	int a, b;//局部变量仅仅是定义,未赋值
	//cout << a << " " << b;在VS里面会报错,但是在Qt里面,每次就会打印相同的值

	cin >> a >> b;
	swap1(&a, &b);
	cout << a << " " << b << endl;

	cin >> a >> b;//重新赋初值
	swap2(a, b);//不需要加&
	cout << a << " " << b << endl;

	cin >> a >> b;
	swap3(a, b);
	cout << a << " " << b << endl;


	system("pause");
	return 0;
}

void swap1(int* p1, int* p2) {
    
    
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

void swap2(int& r1, int& r2) {
    
    
	int temp = r1;
	r1 = r2;
	r2 = temp;
}

void swap3(int a, int b) {
    
    
	int temp = a;
	a = b;
	b = temp;
}

C++ reference vs pointer

  1. There is no null reference. The reference must be connected to a valid piece of memory.
  2. Once a reference is initialized to an object, it cannot be pointed to another object. The pointer can point to another object at any time.
  3. The reference must be initialized when it is created. The pointer can be initialized at any time.

Guess you like

Origin blog.csdn.net/qq_43641765/article/details/112471776