C ++ function return reference

Source: Internet
Author: User

I. The return of C ++ functions is divided into the following situations:

1) Return Value of the main function: Here, 0 indicates that the program runs successfully.

2) return non-reference type: the return value of the function is used to initialize the temporary object created by the hop function. The method for initializing temporary objects with function return values is the same as that for initializing parameters with real parameters. If the return type is not a reference, the return value of the function is copied to the temporary object where the function is called. The returned value can be either a local object or a solution expression.

3) return reference: When the function returns the reference type, the return value is not copied. Instead, the object itself is returned.

2. function return reference

1. When the function returns the reference type, the return value is not copied. Instead, the object itself is returned. Let's take a look at two examples. Example 1 is as follows:

Const string & shorterstring (const string & S1, const string & S2)

{

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

}

Example 2:

[CPP]
View plaincopyprint?
  1. Ostream & operator <(ostream & output, const AAA & AAA)
  2. {
  3. Output <AAA. x <''<AAA. Y <
    ''<AAA. z <Endl;
  4. Return output;
  5. }

Ostream & operator <(ostream & output, const AAA & AAA) <br/>{< br/> output <AAA. x <''<AAA. Y <''<AAA. z <Endl; <br/> return output; <br/>}

Both the form parameter and the return type point to the reference of the const String object. When you call a function or return a result, these string objects are not copied.

2. Return reference, which must be included in the function parametersReferenceMethod orPointerMethod, parameters to be returned. For example:

Int& ABC (IntA,
IntB,IntC,Int& Result ){

Result = A + B + C;
ReturnResult;
}

This form can also be rewritten:

Int& ABC (IntA,
IntB,IntC,Int* Result ){
* Result = A + B + C;
Return* Result;
}

However, the following form is not acceptable:
Int& ABC (IntA,
IntB,IntC ){
ReturnA + B + C;
}

3. Do not return references to local objects. When the function execution is complete, the storage space allocated to the local object is released. In this case, the reference to a local object will point to an uncertain memory. For example:

Const string & manip (const string & S)

{

String ret = s;

Return ret; // wrong: Returning reference to a local object

}

4. Return the left value for the reference. The referenced function returns a left value. Therefore, such a function can be used wherever the left value is required. For example, see C ++ primer p215.

5. Because the return value directly points to a variable whose life cycle has not ended, any operation on the function return value (or function result) itself is actually, is the operation on that variable, which is the IntroductionConstType. WhenConstAfter the keyword, it means that the return value of the function cannot be modified immediately! The following code cannot be compiled. This is because the return value immediately performs the ++ operation (equivalent to the ++ operation on the variable z). For this function, is not allowed. If you remove the const and compile it again, you can get it and print it to formZ
= 7
.

Include<Iostream>
Include<Cstdlib>
Const int& ABC (IntA,
IntB,IntC,Int& Result ){
Result = A + B + C;
ReturnResult;
}


IntMain (){
IntA = 1;IntB = 2;
IntC = 3;
IntZ;
ABC (a, B, c, Z) ++; // wrong: returning a const reference
Cout <"z =" <z <Endl;
System ("pause ");
Return0;
}

Iii. Thinking:

1. When is the returned reference correct? When is the returned const reference correct?

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

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.