C++ reference learning


foreword

This article refers to:
[C++] An article that takes you to understand references (&) -- Introduction to C++

1. What is a reference?

You can understand it as an alias, but it still represents the same thing. A reference is an alias for an existing variable, which shares the same memory space as the variable it refers to, and the compiler will not open up a new memory space for the reference variable.

1.1. Representation of references

类型 &引用变量名(别名) = 引用实体

code:

int main()
{
    
    
	int a = 10;
	int& b = a;
	int& c = b;
	cout << "a的地址" << &a << endl;
	cout << "b的地址" << &b << endl;
	cout << "c的地址" << &c << endl;
	return 0;
}

Both b and c are aliases of a, and all three of them point to the same memory space, we can print out their addresses for verification.

insert image description here


1.2, Citation Properties

References must be initialized when they are defined

#include<iostream>
using namespace std;
 
int main(){
    
    
	int a=10;
	int& ra;//编译时出错
    return 0;
}

An entity can have multiple references

int main()
{
    
    
	int a = 10;
	int& b = a;
	int& c = a;
	int& d = a;
	cout << "a的地址" << &a << endl;
	cout << "b的地址" << &b << endl;
	cout << "c的地址" << &c << endl;
	cout << "d的地址" << &d << endl;
	return 0;
}

The same reference cannot refer to different entities

int main()
{
    
    
	int a = 10;
	int b = 20;
	int& c = a;//引用实体a
	cout << "a: " << a << endl;
	c = b;//再引用实体b
	cout << "c: " << c << endl;
	cout << "a: " << a << endl;
	return 0;
}

When a reference already refers to an entity, it becomes an assignment operation to refer to another entity.
insert image description here

1.3. Reference authority

Permissions can be narrowed but not expanded when citing

int main()
{
    
    
	int a = 10;
	const int b = 20;
	int& a1 = a;//权限不变,可以。
    const int& c = a;//权限缩小,可以。
	int& d = b;//权限扩大,编译报错。
	const int& b1 = b;//权限不变,可以。
	return 0;
}

When an implicit conversion occurs, a const reference can be used


int main(){
    
    
	double a=3.14;
	//int& ra=a;//权限其实是扩大了的,此时编译出错
	const int& rb = a;//可以
    return 0;
}

When an implicit conversion occurs, a temporary variable will actually be generated, and the temporary variable has the characteristics of a constant, so a constant reference needs to be used.

1.4. Usage scenarios

1. Make parameters

void Swap(int &x, int &y)
{
    
    
	int tmp = x;
	x = y;
	y = tmp;
}

int main()
{
    
    
	int a = 10;
	int b = 20;
	cout << "a: " << a << endl;
	cout << "b: " << b << endl;
	Swap(a, b);
	cout << "a: " << a << endl;
	cout << "b: " << b << endl;
	return 0;
}

insert image description here
Different from the cumbersome addressing operation in C, in C++, references can be used directly to complete the actual parameter exchange function. At this time, the reference is itself, and no temporary variable is generated.
2. Do the return value
First, let's look at a simple program:

	int Count()
	{
    
    
	   int n = 0;
		n++;
		//cout << &n << endl;
		return n;
	}
	int main()
	{
    
    
		int num = Count();
		cout << num << endl;
		//cout << &num << endl;

		return 0;
	}

Obviously, the value of num is 1. This is when the program is running, a temporary variable will be generated when the value is returned, and this temporary variable will be assigned to num. Because temporary variables have constant attributes, when using a reference to receive, you need to use a constant reference to avoid permission expansion.

	int main()
	{
    
    
		//int & num = Count();//编译报错
		const int& num = Count();
		//cout << num << endl;
		//cout << &num << endl;

		return 0;
	}

But what happens when you add a reference to the Count function?

	int & Count()
	{
    
    
	   int n = 0;
		n++;
		cout << &n << endl;
		return n;
	}
	int main()
	{
    
    
		int & num = Count();
		const int& num1 = Count();
		cout <<"num的值: " << num << endl;
		cout <<"num的地址: " << &num << endl;
		cout << "num1的值: " << num1 << endl;
		cout << "num1的地址: " << &num1 << endl;
		return 0;
	}

In the above code, the return value of the reference type is used. At this time, it can be considered that a reference (alias) with the temporary variable n is generated, and then the alias is assigned to num and num1. At this time, the permissions can be unchanged or reduced. We can verify this by printing the address.
insert image description here
In fact, the above code is unreasonable. After calling the Count function, the function will be destroyed. At this time, accessing this space by reference is actually illegal access. The consequences of illegal access will lead to wild pointers. If you call another function, the value of num is likely to change.

int& Count()
{
    
    
    //static int n = 0; 这样可以
	int n = 0; // 这样不好
	n++;
	//cout << "n:"<< & n << endl;

	return n;
}
int main()
{
    
    
	int& num = Count();
	cout << num << endl;
	cout << "num" << & num << endl;
	cout << num << endl;


	return 0;
}

When I tested with vs2022, I found that the problem of wild pointers would no longer occur, but it is still not recommended to write like this. We can use static to modify variables and store variables in the static area to prevent future troubles.
insert image description here

1.4. Efficiency Description

Values ​​are used as parameters or return value types. During parameter passing and return, the function does not directly pass actual parameters or return the variable itself directly, but passes actual parameters or returns a temporary copy of the variable, so values ​​are used as parameters Or the return value type is very inefficient, especially when the parameter or return value type is very large, the efficiency is even lower.

Summarize

This article is some small knowledge of c++ references, I hope it can help you.

Guess you like

Origin blog.csdn.net/Done_for_me/article/details/129946557