C + + functions return problems with references, non-references, and temporary variables

Source: Internet
Author: User
Tags fun call

The reference type is new in C + +, so the return value of the function can be a reference type. Then someone would ask. Is there a difference between a return reference type and a return non-reference type?

The conclusion is obvious, and there is a clear difference. In particular, beginners can easily get around. Let's take a look at four function prototypes first. example with int type

(1) int fun (...)

{

Return ....//followed by a reference

}

Example: int fun (int &a)

{

return A;

}

(2) int fun (...)

{

Return....//is followed by a non-reference

}

For example: int fun (int a)

{

return A;

}

(3) int& Fun (...)

{

Return....//is followed by a reference

}

(4) int& fun (...)

{

Return....//is followed by a non-reference

}

The above four functions, (1) (2) are functions that return non-reference types, (3) (4) are functions that return reference types. for which type of function is returned, we want to look at the type of argument in front of the function name, not the type after the return of the function body, remember!!!

C + + Specifies that when a function returns a non-reference type, the function creates a temporary object (temporary. Object), and the function returns the temporary objects. When solving an expression, if you need a place to store its results, the compiler creates an unnamed object, which is the temporary object. In plain words, when you call a function, the function will return a value so that the value must have a place to store it, the compiler will put the value in a non-named temporary object.

To see a simple program

Corresponds to (2)
#include <iostream>
int fun (int &a)
{
int b=a;
return b;
}
int main ()
{
int a=8;
int B=fun (a);//***
return 0;
}

int B=fun (a); Parse this line code: Because fun is to return a non-reference function, call the fun function, the function body executes a return, you need to create a temp variable temp, equivalent to the execution of the int temp, and then assign the value of return to temp, equivalent to the execution of temp =b And then initialize B with the value of temp, which is equivalent to executing an int b=temp.

int B=fun (a); add to int temp;temp=b;int b=temp;

When this expression ends, temp will die, so you cannot int& B=fun (a); Temp is in the fun of the local variable, fun call, after the end of the expression, temp dies, b reference to the variable temp does not exist, so the compilation will not pass.

Corresponds to (1)

#include <iostream>
int fun (int &a)
{
return A;
}
int main ()
{
int a=8;
int B=fun (a);//***
return 0;
}

int B=fun (a); Parse this line code: ditto, equal to int temp;temp=a//assigns a value to Temp;int b=temp//with a variable referenced by a, and initializes B with temp, and when the expression ends, temp dies.

Second,C + + specifies that when a function returns a reference type. C++primer says the return value is not copied when the function returns a reference type. Instead, the object itself is returned.

Find longer of strings

Const string &shorterstring (const string &s1,const string &s2)

{

Return S1.size () <s2.size ()? s1:s2;

Both the}//and return types are references to the Const string object, and the string objects are not copied when the function is called and the result is returned.

For example, the following function, please analyze:

int A;

int& Fun ()

{

return A;

}//because A is a global variable, the return value of a function is a itself, and a has a global scope. If a is a local variable within a function, the function will have an error.

According to C++primer, the return is a this whole play variable itself. In order to be consistent with returning non-referencing functions, I understand that the return is a reference to the global variable, that is, int b=fun (a); I understand the int &temp=a;int b=temp. Perhaps this understanding is a bit farfetched. The function returns a reference, which is the alias of the variable, and can be equated with the object that initializes it, referencing the = variable (initializing the referenced variable), so the function returns even if the A variable itself.

int A;

int& Fun ()

{

int &b=a;

return b;

}//a alias B, but this alias can only be used in the fun function body, out of the fun function body will die, the function return value is a reference, then the return is actually a This variable!!!

Never return a reference to a local object

int &fun (int &a)

{

int b=a;

return b;

}

This function runs with an error because it returns a reference to the local object. When the function is executed, the storage space occupied by int B is freed, and the function return value points to the memory space that is no longer valid for this program.

A good way to make sure that you return a reference security is to ask yourself, which point does this reference refer to which object existed before?

Assign a return reference and a non-referenced function to another variable

When a function returns a value (int Func1 ()), a temporary variable is generated as a copy of the function return value (the value to be returned in the function call is saved), and a copy of the value (int &func2 ()) is not produced when the reference returns a value (int.). Therefore, when a reference is defined with a function return value (int &ia = FUNC1 ()), the reference is a reference to a temporary variable, and because the scope of the temporary variable is short-lived, the reference is at risk of being invalid at any time, and is not legal. When using a reference return value, the int &i = FUNC2 () is not produced because it does not produce a copy of the value, and the call in the case of the return value of Func2 () is a static or global variable, which initializes the reference I directly with a variable in the global data area. Of course, if the return value in FUNC2 is a local variable, then of course it is illegal.

You may want to analyze the following four function calls:

Conditions:
int Func1 ();//assumed to be a legal function
int &func2 ();//assumed to be a legal function

Case
int &i = FUNC2 ();//reference I refers to a variable with a scope greater than or equal to int &i, legal
int &ia = FUNC1 ();//reference to IA refers to temporary variable, illegal
int IB = FUNC1 ();//Temp variable initialize IB, legal
int ic = FUNC2 ();//func2 returns the object (variable) itself initializes the IC;
Remember that this seems to be the can of the C + + tutorial inside the topic, should not remember wrong.

C + + functions return problems with references, non-references, and temporary variables

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.