Reference types and comparison with pointers (C++ notes)

Reference is a new mechanism extended by C++ on the C language, and it is a new way of realizing the function of pointers in the C language.

Reference type
. Reference (&) is an alias for an identifier, that is, an alias for a variable.
When defining a reference, it must be initialized at the same time so that it points to an existing object.

int i=10int& ri=i;//定义int引用ri,并初始化为变量i的引用

At this time ri is equivalent to another alias of variable i, operation ri is equivalent to operation i;

int i = 10;
int& ri = i;
ri--;
cout << ri << endl;
cout << i << endl;
//结果是9 9

After the reference is initialized, it cannot be allowed to point to other objects.

The similarities and differences with pointers are the
same:
1. Both are the concept of addresses. The pointer points to a piece of memory, and the content of the internal storage is the address of the pointed memory; the reference is the alias of the module memory.
2. Both references and pointers occupy 4 bytes of storage space in memory. Because they store the address of the pointed object.

Differences:
1. The pointer is an entity and is stored at another address in the memory, but the reference is only an alias of the variable, and its address is the address of the pointed variable.
2. When using both to manipulate the original object, there is no need to dereference (*) when using references, and pointers need to be dereferenced; references are more beautiful in form than pointers. When using the content pointed to by references, you can directly use the reference variable name. Instead of using ** like a pointer; you don't need to use & for addressing like a pointer when defining a reference.
3. References cannot be made to point to other objects after initialization, but pointers can.

	int i = 10,j=5;
	int& r = i;
	int* l = &i;
	r = j;//可行,但r所指变量依旧是i,这里只是把j的值赋予了i(r);
	&r = j;//报错
	l = &j;//可行,使l指向了j

4. References are not const, pointers are const, and pointers modified by const cannot point to other objects. (I will talk about const later)
5. References cannot be null, they need to be initialized when they are defined; pointers can be null.
6. The meaning of using sizeof for references and pointers is different, sizeof (reference) gets the object that refers to the object pointed to, but sizeof (pointer) gets the size of the pointer itself.

	char c = '0';
	char& rc = c;
	char* lc = &c;
	cout << sizeof(rc)<< endl;
	cout << sizeof(lc) << endl;
	//结果是1 4

7. Reference and pointer auto-increment (++) operations have different meanings;
operating reference is equivalent to operating the original variable, and its essence is the auto-increment operation of the original variable;
but the auto-increment of the pointer pointer moves the pointer down one bit.


When to use references and when to use pointers
There are two main reasons for using reference parameters:

The programmer can modify the data object in the calling function.
By passing a reference instead of the entire data-object, the running speed of the program can be improved.
General principles:

For functions that use referenced values ​​without modification:

If the data object is very small, such as a built-in data type or small structure, pass by value.
If the data object is an array, use a pointer (the only option), and the pointer is declared as a pointer to const.
If the data object is a larger structure, The use of const pointers or references has improved the efficiency of the program. This can save the time and space required for the structure.
If the data object is a class object, use a const reference (the standard way to pass class object parameters is to pass by reference)

For functions that modify the data in the function:

If the data is a built-in data type, use a pointer.
If the data object is an array, you can only use a pointer.
If the data object is a structure, use a reference or a pointer.
If the data is a class object, use a reference.

Author: HawardScut
Source: CSDN
Original: https: //blog.csdn.net/hao5335156/article/details/53893714
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qq_43530773/article/details/113770351