C + + function return study [go]

Source: Internet
Author: User

One, the return of C + + functions is divided into the following cases

1) Main function The return value of main: a point mentioned here, return 0 indicates that the program is running successfully.

2) Returns a non-reference type: The return value of the function is used to initialize the temporary object created in the Skip function. Initializing a temporary object with a function return value is the same as the method of initializing the taxiing parameter with an 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 solving an expression.

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

Second, the function returns a reference

1, the return value is not copied when the function returns a reference type. Instead, the object itself is returned. Look at the two examples first, example 1 below:

Const string &shorterstring (conststring &s1,conststring &  S2) {    return s1.size < s2.size s1:s2;}

Example 2:

[CPP]View PlainCopy
    1. Ostream &operator<< (ostream &output, const AAA &AAA)
    2. {
    3. Output << aaa.x << ' << aaa.y << ' << aaa.z << Endl;
    4. return output;
    5. }

Both the formal parameter and the return type are references to the Const string object, and the string objects are not copied when the function is called and the result is returned.

2, the return reference, required in the parameters of the function, contains a reference or pointer to the existence of a parameter that needs to be returned. 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 finishes executing, the storage space allocated to the local object is freed. At this point, a reference to the local object points to the 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 an lvalue for the referenced function. Therefore, such a function can be used in any place where the left value is required. See example: C + + Primer p215

5, since the return value points directly to a variable whose lifetime has not yet ended, any action on the function return value (or the function result) itself is, in fact, the operation of that variable, which is the meaning of the return of the introduced const type. 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 the + + operation (equivalent to the variable z + + operation), which is not allowed for the function. If you remove the const and then compile, you can get through and print results that form 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 is the return reference correct? And when does the return const reference be correct?

Returns a reference to an object that already existed before the function call was correct. When you do not want the returned object to be modified, the return const reference is correct.

MYTEST1:

The native test, the function returns the problem. Found in Xcode on Mac, the return directly returns local variables, and is not copied, but is replaced directly.

////main.cpp//Testvector////Created by New_life on 2017/4/19.//Copyright 2017 chenhuan001. All rights reserved.//#include<iostream>classA { Public: A () {std::cout<<"A construct"<<Std::endl; }        ~A () {std::cout<<"A destory"<<Std::endl; } A (Consta&a) {std::cout<<"A Copy"<<Std::endl; } A&operator= (Consta&a) {std::cout<<"A ="<<Std::endl; return* This; }    intAA;};    A Test () {a A; Std::cout<<"A:"<< &a <<Std::endl;    A B (a); Std::cout<<"B:"<< &b <<Std::endl; returnb;}intMainintargcConst Char*argv[]) {A C= Test ();//The compiler did the optimization. //A C (Testvector ());Std::cout <<"C:"<< &c <<Std::endl;    A D; Std::cout<<"D:"<< &d <<Std::endl; //std::cout << c << Std::endl;    return 0;}

Operation Result:

0x7fff5fbff6b80x7fff5fbff7380x7fff5fbff7380x7fff5fbff728  A Destorya destory

As you can see from the results, the addresses of B and C are the same.

and the local variable B within the function does not have a destructor.

(conjecture, compiler Optimizations)

MYTEST2:

////main.cpp//Testvector////Created by New_life on 2017/4/19.//Copyright 2017 chenhuan001. All rights reserved.//#include<iostream>classA { Public: A () {std::cout<<"A construct"<<Std::endl; }        ~A () {std::cout<<"A destory"<<Std::endl; } A (Consta&a) {std::cout<<"A Copy"<<Std::endl; } A&operator= (Consta&a) {std::cout<<"A ="<<Std::endl; return* This; }    intAA;}; A&Testvector () {a A; Std::cout<<"A:"<< &a <<Std::endl;    A B (a); Std::cout<<"B:"<< &b <<Std::endl; returnb;}intMainintargcConst Char*argv[]) {A& c = Testvector ();//The compiler did the optimization. //A C (Testvector ());Std::cout <<"C:"<< &c <<Std::endl;    A D; Std::cout<<"D:"<< &d <<Std::endl; //std::cout << c << Std::endl;    return 0;}

Results:

0x7fff5fbff6b80x7fff5fbff6a80x7fff5fbff6a80x7fff5fbff730  A destory

In this experiment, the function returns a reference, and you can see that C points to unknown memory.

C + + function return study [go]

Related Article

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.