Supplementary description of references in C ++

Source: Internet
Author: User

 

# Include <iostream>
# Include <string>
Using namespace STD;

Void main (INT argc, char * argv [])
{
Int A = 10;
Int B = 20;
Int & Rn =;
Cout <rn <"|" <A <Endl;
Cout <& rn <"|" <& A <Endl; // The referenced memory address cannot be obtained in C ++, the referenced address is the target address!
Rn = B; // point the reference to another target ---- variable B
Cout <& rn <"|" <& A <"|" <& B <Endl;
Rn = 100; // try to change the value of B
Cout <A <"|" <B <Endl; // output the modified result.
Cin. Get ();
}

Since the reference itself is an alias of the target, the reference address is meaningless, so the referenced memory address cannot be obtained in C ++. The referenced address is the target address. c ++ itself does not provide a method to obtain the referenced memory address.

Once the reference is initialized, it will not be able to be directed to other targets. Although the compilation will not go wrong, the operation will not work, but it actually points to the first target.

In the above Code, Rn = B is actually a = B on the computer, so the value of A is modified.

# Include <iostream>
# Include <string>
Using namespace STD;

Void main (INT argc, char * argv [])
{
Int A = 10;
Void & Rn = A; // The void is of no type.
Int A [100];
Int & RA [100] = A; // error. The referenced array cannot be declared.
Cin. Get ();
}

The above two errors should remember the reference feature. Void modifier cannot declare the reference, and the reference cannot declare the array, that is, the reference array cannot be declared.

Next, let's talk about it. It is also the most important and important content to be mastered in addition to the memory status of traditional function operations.

Here is an example:

# Include <iostream>
# Include <string>
Using namespace STD;

Float C;
Float test (float, float );
Void main (INT argc, char * argv [])
{
Float Pn = test (3.0f, 1.2f );
Cout <PN;
Cin. Get ();
}

Float test (float a, float B)
{
C = A * B;
Return C;
}

In the above Code, we may think that the function returns the C variable. This may be wrong. Under normal circumstances, when we return normal values in the function, a temporary variable temp is automatically generated in the memory stack space, it is a copy of the returned value and a copy generated by the function during return.

The data in the memory is as follows:

It clearly indicates the copy of consular variables.

Next let's look at a situation where the return value is assigned to the reference:

# Include <iostream>
# Include <string>
Using namespace STD;

Float C;
Float test (float, float );
Void main (INT argc, char * argv [])
{
Float & Pn = test (3.0f, 1.2f); // warning: a temporary variable is returned, and the PN reference will be the alias of the Temporary Variable!
Cout <PN;
Cin. Get ();
}

Float test (float a, float B)
{
C = A * B;
Return C;
}

Float & Pn = test (3.0f, 1.2f); this statement can be compiled in BC because the BC extension is set as a reference to the temporary variable setting, the life cycle of the temporary variable will be the same as the referenced life cycle, but it cannot be compiled in VC, because a temporary variable disappears into the stack space after test () is executed, at this time, PN will become a reference with no clear target, and memory errors will occur in severe cases.

For details about the memory usage, see:

As we can see in the figure, because the function is still normal method return, there will still be a temporary copy variable generated, but this time only returns a target address, in main, the target address is assigned a reference Pn.

Next, let's look at another situation. This is the case where the referenced variable is returned:

# Include <iostream>
# Include <string>
Using namespace STD;

Float C;
Float & Test (float, float );
Void main (INT argc, char * argv [])
{
Float Pn = test (3.0f, 1.2f );
Cout <PN;
Cin. Get ();
}

Float & Test (float a, float B)
{
C = A * B;
Return C;
}

In this case, the stack space where test () is located does not generate a temporary variable in the memory, but directly gives the value of global variable C to the variable Pn, this method is the most recommended operation method, because the direct assignment of values without temporary variables can save memory space and improve efficiency, and the readability of the program is also better.

For details about the memory usage, see:

The last case is when the function returns a reference and assigns the value to a reference:

# Include <iostream>
# Include <string>
Using namespace STD;

Float C;
Float & Test (float, float );
Void main (INT argc, char * argv [])
{
Float & Pn = test (3.0f, 1.2f );
Cout <PN;
Cin. Get ();
}

Float & Test (float a, float B)
{
C = A * B;
Return C;
}

In this case, no temporary variables are generated, and the reading and performance are good, but it is easy to make a mistake, that is, when C is a non-main local variable, or is temporarily opened in the heap memory, and is then destroyed, this situation is as serious as the result of the returned pointer being a local pointer, this causes the reference to point to an ambiguous address. For details about this situation in the memory, see:

Because of the scope issue, we recommend that you use the third method.

 

Next we will talk about several examples of using reference as the left value to participate in the calculation. This 1.1 is very important and is very helpful for understanding the function that returns the reference.

# Include <iostream>
# Include <string>
Using namespace STD;

Float C;
Float & Test (float, float );
Void main (INT argc, char * argv [])
{
Float & Pn = test (3.0f, 1.2f );
Cout <PN <Endl;
Test (3.0f, 1.2f) = 12.1; // calculate the left value of the function!
Cout <PN;
Cin. Get ();
}

Float & Test (float a, float B)
{
C = A * B;
Return C;
}

Generally, a function cannot be used as the left value. Because a reference can be used as the left value, the referenced function can be returned as the left value.

In the above Code:

Float & Pn = test (3.0f, 1.2f );

At this point, the Pn has directed to the address of the target c.

Next we run

Test (3.0f, 1.2f) = 12.1;

Calculate the left value of the function. Here, because test returns the referenced function, the address returned by the return value is the address of C. Naturally, the value of C is changed to 12.1.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.