"C++" reference details

  • Referenced concept

Reference does not define a new variable, but takes an alias for an existing variable. The compiler will not open up space for the reference variable. It shares a memory space with the variable it references.

The method of use is as follows:

int main()
{
    
    
	int a=10;
	int &b=a;//定义引用类型

	printf("%d\n",a);
	printf("%d\n",b);
	printf("%p\n",&a);
	printf("%p\n",&b);
	return 0;
}

Run it and see the results as follows. It is clear that the addresses of the two are the same
Insert picture description here
. One thing to note is that the reference type must be the same type as the reference entity. For example, the code a above is of type int, so the reference type must also be of type int.

  • Reference characteristics

1. The reference must be initialized when it is defined, such as int &a; if this sentence appears in your code, the compiler will report
an error 2. A variable can have multiple references, this sentence is easy to understand, just like a person can have Multiple nicknames, maybe your classmates call you XXX, your family members at home call you XX, one reason
3. Once you cite an entity, you can no longer cite other entities

  • Often quoted

Look at a set of questions

//前提:
const int a = 10;
const int b = 10;
int c = 10;
double d = 1.11;

//下面这些代码谁对谁错??
int &r1 = a;
const int &r2 = b;
int r3 = b;
int *p1 = &b ;
const int *p2 = &b ;
int *p3=&c ;
const *p4=&c ;
int &r4 = d;
const int &r4 = d;

Analyze one by one. Before that, let’s talk about an assignment rule. The permissions can be reduced but the permissions cannot be enlarged
. 1. Wrong, a cannot be changed at will by itself, and cannot be changed at will after being quoted
2. Correct, b cannot be changed at will, quote can not arbitrarily change the
3 correct, but the value b is assigned to r3, r3 even if the change is also independent of the b
4. error, b can not change, does not have permission to enlarge
5. correctly, with 2
6. correctly, can freely C Change, the same after quoting
7. Correct, permission can be reduced
8. Wrong, d is a double type, r4 actually refers to the temporary variable in the middle
9. Correct

  • Referenced usage scenarios

1. Make parameters

Explain with a simple code

void swap(&a,&b)
{
    
    
	int c=a;
	a=b;
	b=c;
}

int main()
{
    
    
	int x1=10,x2=20;
	swap(x1,x2);
	
	cout<<x1<<endl;
	cout<<x2<<endl;
	return 0;
}

2. As the return value

Can this code run?

int Arr(int i)//改成int &Arr(int i)
{
    
    
	static int a[10]={
    
    0,1,2,3,4,5};
	return a[i];
}

int main()
{
    
    
	Arr(3)=10;
	cout<<Arr(3)<<endl;
	
	return 0;
}

It can’t run, because when you assign 10 to Arr(3), you don’t actually assign a value of 10 to a[3], but assign a value of 10 to its copy. Changing its copy does not affect the body of a[3]. influences. After modification, it is fine, because at this time, the reference of a[3] is assigned a value of 10, and his reference is changed, and the ontology is also modified, so it is feasible.
One more point, is it feasible if you delete static? Not feasible, because if you delete static, the return value will be destroyed if the return value goes out of scope, that is, the function Arr().
So what do you think is the output of the following code? 3? In fact, it is 7. The reason is the same as above. When the function returns, after leaving the function scope, the stack has been returned to the system, so the space on the stack cannot be returned as a reference type. If it is returned as a reference type, the lifetime of the return value must not be limited by the function.

int &Add(int a,int b)
{
    
    
	int c=a+b;
	return c;
}

int main()
{
    
    
	int &ret =Add(1,2);
	Add(3,4);
	cout<<"Add(1,2) is:"<<ret<<endl;
	return 0;
}

We all know that the value is used as a parameter or return value type. During the transfer and return, the function does not directly pass the actual parameter or return the variable itself directly, but passes the formal parameter, which is a temporary copy of the actual parameter, which is efficient It is very inferior, especially when the parameter or return value type is very large, the efficiency is lower, so we are more efficient when passing by value and returning by reference.

  • The difference between references and pointers

As mentioned earlier, a reference is an alias, there is no independent space, and it shares a space with the referenced entity, but in fact, there is room for the underlying implementation of the reference, because the reference is implemented in the way of pointers.

So what is the difference between references and pointers?

  • The reference must be initialized in the definition, and the pointer is not required
  • A pointer can point to any entity of the same type at any time, but references cannot
  • No NULL references but NULL pointers
  • The meaning is different in sizeof, the reference result is the size of the reference type, and the pointer is always the number of bytes occupied by the address space (32-bit platform occupies 4 bytes)
  • There are multi-level pointers, no multi-level references
  • The way to access the entity is different, the pointer needs to be explicitly dereferenced, and the reference is handled by the compiler itself

It seems that pointers are more useful than references, but why do they still need to be used a lot? Because references are relatively safer to use than pointers

Guess you like

Origin blog.csdn.net/NanlinW/article/details/103051574