First, I want to explain the title of the article. It sounds a bit difficult, meaning:Functions with return values, what are done during the call and return(These things are often invisible to the Code ).
I. Principles
For the return value of a function, let's talk about the return statement. The return statement ends the currently executed function and returns the control to the function that calls the function. YesTwo Forms:
1. return; // void in a function without return values)
Return statements without return values can only be used to return void-type functions. However, note: In a function with the return type void, the return statement is not required. Implicit return occurs when the last statement of the function is completed.
In general, the return statement is used for a function with the return type void to force the function to end (similar to the break statement in the loop structure ).
2. return expression: // In a function with a returned value (non-void)
In this case, the return value type must be the same as the return type of the function, or be implicitly converted to the return type of the function.
Function return value: used to initializeTemporary object(Temporary object ).
For a function with a return value, when calling a function (such as solving an expression), a local memory operation result is required. At this time, the compiler creates an unnamed object ----- temporary object.
For example:
...int add(int a, int b){ return a + b ;}....int a = 1;int b = 2;int c = add(a, b); //create temporary, delete it after executing this statement...
In the above Code, the following occurs when int c = add (a, B:
- First, create a temporary object, such as temp.
- Then, copy the copy of the returned value A + B of the function to temp;
- Finally, copy the temp copy to variable C and delete the Temporary Variable temp.
Note: In English, C ++ programmers usually use the term temporary to replace temporary object.
Ii. Example
To better understand the function with a returned value, the following describes the running status of a class:
#include <iostream>using namespace std;class A{private:int m_x;public:A(int x=0):m_x(x){cout<<"constructor, value: "<<m_x<<endl;}A(const A& a){m_x = a.m_x;cout<<"copy constructor, value: "<<m_x<<endl;}A& operator=(const A& a){m_x = a.m_x;cout<<"assignment operator, value: "<<m_x<<endl;return *this;}~A(){cout<<"destructor, value: "<<m_x<<endl;}A getOneObject(int x){cout<<"in fun call"<<endl;A a(0);a.m_x =x;return a;}};int main(){A a1(1);A a2(2);cout<<"before fun call"<<endl;a2= a1.getOneObject(3);cout<<"after fun call"<<endl;cout<<"before quit"<<endl;return 0;}
Pay attention to the changes in output results.:
Note: The above Program Main is not an efficient writing method. You can use [programmer optimization] (For details, refer to program conversion semantics in C ++ object model to optimize it at the user level: bp65)
The main function can be modified as follows:
int main(){A a1(1);//A a2(2);cout<<"before fun call"<<endl;A a2= a1.getOneObject(3);cout<<"after fun call"<<endl;cout<<"before quit"<<endl;return 0;}
Output result:
Refer:
C ++ primer Chinese edition P211-P215
Deep Exploration C ++ object model P60-P69