[C++] Basic concepts and usage scenarios of citations


Preface

This article will introduce a new syntax reference introduced by C++ compared to the C language . –The article mainly starts fromconcept of citationQuote usage scenariosAnd the differences between references and pointers will be introduced in three aspects.


1. What is a citation?

Quote Just like we give nicknames to our friends, Zhang San is his real name and Goudan is the nickname given to him. When we call Zhang San or Goudan, this person will respond, and the same is true for quotes.
A reference does not define a new variable, but gives an alias to an existing variable. The compiler will not allocate memory space for the reference variable. It shares the same memory space with the variable it refers to.
Syntax: type & reference variable name (alias for entity name) = entity name

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 0;
	//b成为了a的别名,对b的赋值,加减等各种运算也会对a进行相应的改变
	int& b = a;
	//输出a和b两个变量的地址,可以看到是相同的,说明两个变量共用一块空间
	cout << &b << endl;
	cout << &a << endl;
	return 0;
}

Notice! The reference type must be of the same type as the referenced entity


2. Characteristics of citations

1. The reference must be initialized

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 0;
	//int& b;此处编译器会报错,引用必须初始化
	int& b = a;
	return 0;
}

2. Once a reference refers to an entity, it cannot refer to other entities.

Beginners often think that the following code implements a redirection of b to reference c, but in fact it only implements a copy function

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 0;
	int c = 1;
	int& b = a;
	b = c;//把c的值赋给了b
	return 0;
}

3. An entity can have multiple references

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 0;
	//b和m都是a的引用
	int& b = a;
	int& m = a;
	return 0;
}

4.Constant reference

#include <iostream>
using namespace std;
int main()
{
    
    
	const int a = 10;
	//这条语句会报错是因为,a为常量
	//当我们用非常量类型的b去作为a的引用,则a的内容可能会因为b的改变而被修改
	//为了避免这样的情况发生,我们必须用同为常量的类型作为a的引用
	//int& b = a;

	const int& b = a;
	return 0;
}

3. Reference usage scenarios

1. Make parameters

//这样就可以不使用指针而实现两个数的交换
void Swap(int& a, int& b)
{
    
    
	int tem = a;
	a = b;
	b = tem;
}

2. Make the return value

//这样就返回了静态变量c的引用
int& test()
{
    
    
	static int c = 5;
	c++;
	return c;
}

Notice! When a variable goes out of scope and is destroyed, its reference cannot be returned, as shown below

int& test()
{
    
    
	int c = 5;//c出了函数会被销毁,所以不能返回它的引用
	c++;
	return c;
}

4. The difference between references and pointers

1. Grammatical level

A pointer opens up a memory space to store the address of the variable pointed to.
A reference is an alias, it has no independent space, and the subject of the reference shares a space.
It can be seen from the following code that the addresses of reference variables and entities share a space

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 4.0;
	int& b = a;
	cout << "&a = " << &a << endl;
	cout << "&b = " << &b << endl;
	return 0;
}

Insert image description here

2. Underlying implementation

In the underlying implementation, references are actually implemented in the form of pointers.
Translate the following code into assembly code to see that pointers and references are implemented the same way under the hood.

#include <iostream>
using namespace std;
int main()
{
    
    
	int a = 4;
	int& b = a;
	b = 10;

	int* pa = &a;
	*pa = 10;
	return 0;
}

Insert image description hereInsert image description here

3. Other differences

1. Reference conceptually defines an alias of a variable, and a pointer stores a variable address.
2. References must be initialized when defined, and there are no requirements for pointers. 3. After a reference refers to an entity during initialization, it cannot refer to other entities, and a pointer can point to any entity of the same type
at any time . 4. The meaning in sizeof is different: the reference result is the size of the reference type, but the pointer is always the number of bytes occupied by the address space. 6. The reference is incremented, that is, the referenced entity is increased by 1, and the pointer is incremented, that is, the pointer is offset backward by the size of the type. 7. There are multi-level pointers, but there are no multi-level references. 8. The way to access entities is different. The pointer needs to be explicitly dereferenced, and the reference compiler handles it by itself. 9. References are relatively safer to use than pointers.






Summarize

This article introduces the basic concepts, characteristics and usage scenarios of references, as well as the differences between pointers and references. Hope this article will be helpful to you.

Guess you like

Origin blog.csdn.net/weixin_63614711/article/details/130520168