Function return value-return variables and references

Source: Internet
Author: User

-- Convention: start with a simple code

# Include <iostream. h>

Float temp; // defines a global variable.

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

Float & FN2 (float R) // return reference

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

Void main ()
{
Float a = fn1 (5.0 );//
// Float & B = fn1 (5.0); // VC ++ 6.0 error, (other compilers may be warning)

Float c = FN2 (5.0 );//
Float & D = FN2 (5.0); // If temp is not a global variable, an error may still be reported;

}

It is easy to understand that the above program float a = fn1 (5.0) and float c = FN2 (5.0) are normal during compilation. It is a simple value assignment statement, assign the return value of the function to the variable. Float & B = fn1 (5.0) cannot be compiled, but float & = FN2 (5.0) can be passed, but it is a bit confusing, this depends on the runtime structure and the C language architecture.

First, the function return value type in C language is used to allocate space for the function return value during running. This space is allocated to the stack.

For example

Int fun (void) // int is used to tell the compiler to allocate an int space to the return value when function code is running,

{// And the space is allocated in the stack: execute a = fun () equivalent to the following statements:

Int temp; // int temp; int s_temp = temp; A = s_temp;

Return temp; // Where int s_temp = temp is the executable code generated by the compiler for return temp.

} // Equivalent C statement, that is, the compiler creates a local variable in this place to allocate a space for the return value, then, the value of this local variable is given to the variable of the returned value. Therefore, this form is only simple value transfer. Even in the following form, only the Temp value is returned, not the temp itself.

Int temp;

Int fun (void)

{

Return temp;

}

So float & B = fn1 (5.0); the error is caused by referencing a local variable: Float & B = s_temp; s_temp has been destroyed before B is referenced for destruction, this is meaningless, so the compiler should report an error.

Int temp;

Int & fun (void)

{

Return temp; // execute Int & A = fun () to be equivalent to Int & s_temp = temp; Int & A = s_temp;

}

Float & D = FN2 (5.0); the cause of the error is: A is the reference of s_temp, s_temp is the reference of temp, so a is also the reference of temp, so even if the local variable s_temp is deleted, the global variable temp still exists, so a can still reference temp

However, when temp is a local variable, the above error occurs.

Use a class call to verify the following:

 

# Include <iostream>

Using namespace STD;

Class Test
{
Public:
Test ()
{
Cout <"default constructor" <Endl;
}

Test (test & T)
{
Cout <"Default copy constructor" <Endl;
}

~ Test ()
{
Cout <"destructor" <Endl;
}

Test fun1 (Test & T) // The key here: Test temp; execution of Test & t = temp does not call the constructor or copy the constructor ,;
{// Because only an application is created for temp, which is equivalent to a constant pointer and does not open up memory.
Test temp; // defines the local variable to call the default constructor.
Cout <"fun1 called" <Endl;

Return temp; // equivalent: Test s_temp = temp; call the default copy constructor.
}

};

Int main ()
{
Test T1; // call the default constructor
Test T2; // call the default constructor
T1.fun1 (T2); // call the default constructor first, and then call the default copy constructor when the returned value is executed.

Return 0;
}

The execution result is as follows: (because no, it is easy to use it. Based on the execution result, we can see that the above assumptions about the returned value are correct .)

Default constructor

Default constructor

Default constructor

Fun1 called

Default copy constructor

Destructor

Destructor

Destructor

Destructor

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.