C + +: Reference as return value __c++

Source: Internet
Author: User


For example, the following program is about 4 forms of reference return:
//*********************
* * Ch9_6.cpp * *
//*********************
#include <iostream.h>
float temp;
Float fn1 (float R)
{
temp = r*r*3.14;
return temp;
}
float& fn2 (float R)
{
temp = r*r*3.14;
return temp;
}
void Main ()
{
Float A=FN1 (5.0); 1
float& b=fn1 (5.0); 2:warning
Float C=FN2 (5.0); 3
float& d=fn2 (5.0); 4
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
}
The results of the operation are:
78. 5
78. 5
78. 5
78. 5
In the form of 4 references to the main function, the program runs the same result. However, their activity in memory varies from one to the other. Where the variable temp is global data residing in the global data area. function main (), function FNL (), or function fn2 () reside in stack stacks.
In the first case, see Figure 9-5.


Figure 9-5 Memory layout for the return value method
This situation is the normal function return value method. When the global variable temp value is returned, C + + creates a temporary variable and copies the temp value of 78.5 to the temporary variable. After returning to the main function, the assignment statement A=FNL (5.0) Copies the value 78.5 of the temporary variable to a.
The second scenario: see Figure 9-6.


Figure 9-6 Returns the case of the initial reference of the value
In this case, the function FNL () is returned as a value, and when returned, the value of the temp is copied to the temporary variable. After returning to the main function, reference B is initialized with the temporary variable, making B an alias for the temporary variable. Because the scope of the temporary variable is short, B is in danger of being ineffective. According to the C + + standard, the lifetime of a temporary variable or object is terminated after the end of a complete statement expression, that is, after "float& B=FNL (5.0);", the temporary variable no longer exists. So the value after the reference B is a value that cannot be determined. BC extends the C + + standard by stipulating that if a temporary variable or object is initialized as a reference, its lifetime is consistent with that reference. Section 14.7 will further introduce this content. Such a program, depending on the compiler's specific implementation, so the portability is poor.
To initialize a reference with the return value, you should first create a variable, assign the function return value to the variable, and then initialize the reference with that variable, as follows:
int X=FNL (5.0);
int& b=x;
The third scenario: see Figure 9-7.


Figure 9-7 Returning the reference mode
In this case, the return value of the function fn2 () does not produce a copy, so the variable temp is returned directly to the main function. The left value in the assignment statement of the main function is copied directly from the variable temp, thus avoiding the occurrence of the temporary variable. When variable temp is a user-defined type, this approach directly brings the benefit of program execution efficiency and space utilization.
Fourth: See figure 9-8.


Figure 9-8 Returns the value of the reference as a reference initialization
In this case, the function fn2 () returns a reference and therefore does not produce a copy of any returned values. In the main function, a reference declaration D is initialized with the return value, making d the alias for temp. Because temp is a global variable, temp remains in effect for the duration of D. This approach is safe.
However, if you return a reference to a variable or object that is not scoped, there is a problem. This is as serious as returning a local scope pointer. BC as a compilation error, VC as a warning, to draw the attention of programmers. For example, the following code returns a reference to initialize the reference declaration for the main function:
float& fn2 (float R)
{
float temp;
temp=r*r*3.14;
return temp;
}
void Main ()
{
Float &D=FN2 (5.0);//error The reference returned is a local variable
}
See figure 9-9 for a description.


Figure 9-9 The reference returned is a local variable
If the returned reference is performed as a left value, it is also the programmer's taboo. So, if you have the following code in your program, be sure to remove it:
float& fn2 (float R)
{
float temp;
temp=r*r*3.14;
return temp;
}
void Main ()
{
FN2 (5.0) =12.4;//error returns a variable in a local scope
}

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.