C++ super detailed reference [c++]

Table of contents

1. The concept of reference

2. Application Features

Three, often cited

4. Usage scenarios


1. The concept of reference

A reference is not redefining a variable, but an alias for an existing variable. The compiler will not open up a space for applying the variable, but the alias shares a memory space with the existing variable.

For example: Li Kui is called "Tie Niu" at home, but he is called "Black Whirlwind" in Jianghu.

The quoted symbol is "&"

Use Cases

void TestRef()
{
    int a=10;
    int &ra=a;
    printf("%p\n",a);
    printf("%p",ra);
}

 Test Results:

 It means that what they call is the same space.

void Test()
{
    int a = 10;
    printf("%d\n", a);
    int& ra = a;
    printf("a=%d,ra=%d", a, ra);
}

Test Results: 

 Explain that a variable has the same reference authority as him, just like calling him Li Kui has the same effect as calling him Black Whirlwind.

The reference type must be of the same type as the referenced entity.

2. Application Features

1. The reference must be initialized when it is defined, because it cannot change its own reference variable after it is defined.

2. A variable can have multiple references, and a person can also have multiple nicknames.

3. References Once an entity is referenced, no other entities can be referenced.

void TestRef()
{
    int a=10;
    int &ra=a;
    int &rra=a;
    printf("%p   %p   %p   ",a,ra,rra);
}

Test Results:

 Three, often cited

const int a=10;
int &ra=a;

This piece of code will throw an error

The reason is: a can only be read but not written, but you wrote a reference ra to change it from a variable that can only be read to a variable that can be read and written.

Principle: By referring to a variable, you can shift the authority on the authority level, and you can also narrow the authority to the reference.

This code should be changed to:

const int a=10;
const int &ra=a;

How to narrow down the permissions:

int a=10;
const int &ra=a;

There is another important usage of constant references, which is used to store constants:

void Test()
{
    int a = 0;
    double d = 12.34;
    a = d;
}

Although this piece of code will not report an error in the compiler and can be successfully compiled, do you know what it is?

  

 The double type data d will enter the temporary space before being sent to a, and then the a variable will fetch the data in the temporary space , so the data will be truncated and 12 will be stored in a.

This temporary space is constant, so the following code will report an error:

void Test3()
{
    int &a=10;    
}

 

 Because 10 is actually data in a temporary space, the temporary space is constant and needs to be loaded with const references .

 4. Usage scenarios

1. Make parameters

void Swap(int& left, int& right) 
{
   int temp = left;
   left = right;
   right = temp; 
}

This is actually similar to a pointer. The pointer transfers the address in, the value in the function changes, and the value outside the function also changes. The reference passes in the alias of the variable, which is equivalent to passing in the right to use the variable.

2. Do the return value

int& Count()
{
   static int n = 0;
   n++;
   // ...
   return n; 
}
int main()
{
     int   ret=Count();
     return 0; 
}

There is an advantage of using a reference as a return value, which can improve program efficiency.

 After calling the Test4 function, its function stack will also be destroyed, and the variable n will no longer exist. So how did we return the return value to ret after the function ends? The compiler will apply for a register in memory to store the value of n, and then pass the value to ret.

But in the Count function, there is no such process. Static will store n in another space, and it will still exist after the function stack is destroyed. This is to return the reference value directly. You can directly return the value of n to ret2, so that you can Eliminate intermediate steps and improve efficiency.

 

Guess you like

Origin blog.csdn.net/qq_64484137/article/details/126922728