Return Value by reference

Source: Internet
Author: User

When the function returns a value, a copy of the value is generated. When the return value is referenced, a copy of the value is not generated.
For example, the following program provides four forms for returning a reference:
//*********************
// ** Ch9_6.cpp **
//*********************

# Include <iostream. h>

Float temp;

Float fn1 (float R)
{
Temp = r * 3.14;
Return temp;
}

Float & FN2 (float R)
{
Temp = 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 running result is:
78.5
78.5
78.5
78.5

The execution results of the program are the same for the four types of references returned by the main function. However, their activities in the memory are different. The temp variable is global data and resides in the global data zone. The function main (), function FNL (), or function FN2 () reside in the stack.
First case: see Figure 9-5.


Figure 9-5 memory layout in return value mode

This 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 78.5 to the temporary variable. After returning to the main function, the value assignment statement A = FNL (5.0) copies the value of the Temporary Variable 78.5 to.
The second case: see Figure 9-6.


Figure 9-6 initial reference of returned values

In this case, the FNL () function returns the value and copies the Temp value to the temporary variable. Return to the main function, reference B to initialize with this temporary variable, so that B becomes the alias of this temporary variable. Because the scope of temporary variables is short, B is at risk of being invalid. According to the C ++ standard, the life cycle of a temporary variable or object ends after a complete statement expression, that is, after "float & B = FNL (5.0, temporary variables no longer exist. Therefore, the value after B is referenced cannot be determined. BC extends the C ++ standard, specifying that the life cycle of a temporary variable or object as the reference is consistent with that of the reference when it is initialized. Section 14.7 will further introduce this content. Such a program depends on the specific implementation of the compiler, so portability is poor.
To initialize a reference with a return value, you should first create a variable, assign the function return value to this variable, and then initialize the reference with this variable, as shown below:
Int x = FNL (5.0 );
Int & B = X;
Case 3: see Figure 9-7.


Figure 9-7 return Reference Method

In this case, the return value of the function FN2 () does not generate a copy. Therefore, the variable temp is directly returned to the main function. The left value in the Assignment Statement of the main function is copied directly from the temp variable, which avoids the generation of temporary variables. When the variable temp is a user-defined type, this method directly brings the benefits of program execution efficiency and space utilization.
Fourth case: see Figure 9-8.


Figure 9-8 return the value of the reference method as the reference Initialization

In this case, the FN2 () function returns a reference, so no copy of the returned value is generated. In the main function, a reference declaration d uses this return value for initialization, making d an alias for temp. Since temp is a global variable, it is always valid during the validity period of D. This approach is safe.
However, if a variable or object reference that is not within the scope is returned, the problem arises. This is as serious as returning a local scope pointer. BC is used as a compilation error, and VC is used as a warning to draw the attention of programmers. For example, the following code returns a reference to initialize the reference Declaration of the main function:
Float & FN2 (float R)
{
Float temp;
Temp = r * 3.14;
Return temp;
}
Void main ()
{
Float & D = FN2 (5.0); // The reference returned by error is a local variable.
}

See Figure 9-9.


Figure 9-9 references a local variable

If the returned reference is calculated as a left value, it is also the most taboo for programmers. Therefore, if the program contains the following code, you must remove it:
Float & FN2 (float R)
{
Float temp;
Temp = r * 3.14;
Return temp;
}
Void main ()
{
FN2 (5.0) = 12.4; // error returns a variable in the local scope
}

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.