C + + function return reference __ function

Source: Internet
Author: User


Source: http://blog.csdn.net/keyouan2008/article/details/5741917

one, the return of C + + functions is divided into the following situations

1 The return value of the main function main: Here is a reference, and a return of 0 indicates that the program is running successfully.

2 Returns the unreferenced type: The return value of the function is used to initialize the temporary object created by the jump function. Initializing a temporary object with a function return value is the same as the method of starting the Huafing parameter with the argument. If the return type is not a reference, the function return value is copied to the temporary object where the function is called. And its return value can be either a local object or a result of an expression.

3) Return reference: The return value is not replicated when the function returns a reference type. Instead, the object itself is returned.

Two, function return reference

1, the return value is not replicated when the function returns a reference type. Instead, the object itself is returned. Let's look at two examples, example 1 as follows:

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

{

Return S1.size < s2.size? S1:S2;

}

Example 2:

[CPP] view plain copy print? Ostream &operator<< (ostream &output, const AAA &aaa) {output << aaa.x << ' <& Lt        Aaa.y << ' << aaa.z << Endl;   return output; } ostream &operator<< (ostream &output, const AAA &aaa) {output << aaa.x << ' << a Aa.y << ' << aaa.z << Endl; return output; }

Both the formal parameter and the return type are references to a const string object that is not copied when the function is called and the result is returned.

2, returns the reference, requires in the function's parameter, contains has in the reference way or the pointer method existence, needs to be returned the parameter. Like what:

int& ABC (int A, int b, int c, int& result) {

result = a + B + C;
return result;
}


This form can also be rewritten as:


int& ABC (int A, int b, int c, int *result) {
*result = a + B + C;
return *result;
}


However, the following form is not possible:
int& ABC (int A, int b, int c) {
return a + B + C;
}

3, never return a reference to a local object. When the function completes, the storage space assigned to the local object is freed. At this point, a reference to the local object will point to an indeterminate memory. Such as:

Const string &manip (const string &s)

{

string ret =s;

return ret; Wrong:returning reference to a local object

}

4, the reference returns the left value. Returns a reference function that returns a left value. Therefore, such a function can be used in any place where the left value is required. Examples See: C + + Primer p215

5, since the return value directly points to a variable whose lifetime has not yet ended, any operation of the function return value (or function result) itself is, in fact, the operation of that variable, which is the meaning of introducing the const type's return. When the const keyword is used, it means that the return value of the function cannot be modified immediately. The following code will not compile through because the return value is immediately in the + + operation (equivalent to the + + operation of the variable z), which is not allowed for the function. If you remove the const and then compile, you can get through and print the result of z = 7 .

include <iostream>
include <cstdlib>
const int& ABC (int A, int b, int c, int& result) {
result = a + B + C;
return result;
}


int Main () {
int a = 1; int b = 2; int c=3;
int z;
ABC (A, B, C, z) + +; Wrong:returning a const reference
cout << "z=" << z << endl;
SYSTEM ("PAUSE");
return 0;
}

Third, consider:

1, when to return the reference is correct. And when to return the const reference is correct.

It is correct to return a reference to an object that already exists before the function call. It is correct to return a const reference when you do not want the returned object to be modified.

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.