Those things about "quoting"

What

What is the reference?
Everyone has a "nickname" and "milk name" when they are young, such as "dogdan" and "niuniu". The effect of calling your nickname by your parents is the same as calling your name on your household registration book. In fact, calling your nickname The name is more, you also know that "dogdan" and "niuniu" are you and not others.
The reference is not a newly defined variable, but an alias for an existing variable. The compiler does not open up memory space for the reference variable. It shares the same memory with the variable it references.
Type & reference variable name = reference entity;
Insert picture description here

void main()
{
    
    
	int a = 10;
	int& ra = a;
	int ar[10] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int(&br)[10] = ar;
	int* p = &a;
	int*& q = p;
}

Insert picture description here
It should be easy to understand the quote now.
Note:
①Null reference is not allowed, which means that the reference must be initialized when it is defined
; ②A variable can have multiple references, such as int a = 10; int &ra = a; int&rra = a; ③Once the
reference refers to an entity, No more references to other entities;
④The type of the referenced variable name and the entity type must be consistent;

How

1. Passing parameters
Not only can it be used as function parameters, but the efficiency is much higher than that of value-passing calls.

struct Test{
    
    
	int ar[100000];
};

void fun1(Test t)//传址调用
{
    
    

}

void fun2(Test &t)//传引用
{
    
    

}
Test t;
void TestValueTime()
{
    
    
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
		fun1(t);
	time_t end = clock();
	cout << "value time : " << end - begin << endl;
}

void TestRefTime()
{
    
    
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
		fun2(t);
	time_t end = clock();
	cout << "ref time : " << end - begin << endl;
}

void main()
{
    
    
	TestValueTime();
	TestRefTime();
}

Insert picture description here
It can be seen that the efficiency of passing by reference is higher than passing by value. If the structure is more complex, the efficiency is higher.
↑why?
Because when a value is used as a parameter or return value type, the function will not directly pass the actual parameter or return the variable itself directly during the parameter passing and return. Instead, passing the actual parameter will return a temporary copy of the variable, so the value is used As a parameter or return value type, the efficiency is not high.
Second, do return value
First, give the difference between ordinary function return and reference return:
Insert picture description here
efficiency comparison

struct Test{
    
    
	int ar[100000];
};
Test t;
Test fun1()
{
    
    
	return t;
}

Test& fun2()
{
    
    
	return t;
}
void TestValueTime()
{
    
    
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
		fun1();
	time_t end = clock();
	cout << "value time : " << end - begin << endl;
}

void TestRefTime()
{
    
    
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
		fun2();
	time_t end = clock();
	cout << "ref time : " << end - begin << endl;
}

int main()
{
    
    
	TestValueTime();
	TestRefTime();
	return 0;
}

Insert picture description here
To sum up: Whether you use a reference as a parameter or a return value, the efficiency will be improved.

The difference between reference and pointer

In terms of grammatical concept, references are aliases, there is no independent space, and they share a space with the entities they refer to;
there is actually room for the underlying implementation (from the assembly perspective), because references are implemented in the way of pointers. (I don’t understand this sentence very well)
Look at the code:
Insert picture description here
check the assembly code:
Insert picture description here
the difference between a reference and a pointer:
when a reference is defined, it must be initialized and a pointer is not needed;
after referencing an entity, no other entities can be referenced, and the pointer can be in any When pointing to the same type of entity; it
cannot be a null reference, but there is a null pointer; in
sizeof, the reference is the size of the referenced entity type. The pointer occupies 4 bytes in a 32-bit system and 8 bytes in a 64-bit system;
Reference self-addition means that the reference entity is incremented by one, and the pointer self-addition is offset backward by one type size;
multi-level reference is not allowed, but there are multi-level pointers;
reference is safer.

Thank you guys for stepping on it!

Guess you like

Origin blog.csdn.net/qq_43560037/article/details/114670040
Recommended