In almost all c ++ books, it is strongly recommended that the function return by reference, or even directly return pointers, especially when class and struct are involved.
If the function returns an object instead of a pointer, it will use the "Copy to construct" temporary object of the return object when executing the return statement. Then, the Return Statement is executed (a semicolon is encountered) analyze all the variables created in the function to create and output the stack. The temporary object that is constructed by a value assignment is parsed after the statement that calls the function is executed (with a semicolon or braces on the right.
Code:
Class cpcls {public: cpcls (INT) {cout <"cpcls (INT)" <Endl ;}// PRIVATE: // cpcls C2 = 1, if the replication constructor is set to private, G ++ fails to compile cpcls (const cpcls &) {cout <"cpcls (cpcls &)" <Endl ;}}; /* When the function is executed, two temporary variables of the cpcls type are generated: ** 1. the return value is the temporary variable T1 (that is, at the beginning of the function declaration, the place where the declared return value type is cpcls) ** 2. When the return statement is executed, the Temporary Variable T2 (cpcls (1) Statement will generate a temporary variable of the cpcls type) * *** when the return operation is executed, the Temporary Variable T2 is generated by copying the constructor using T2. *** (if you do not believe this, you can set the copy constructor of cpcls to private. ** an error is returned when compiling ~) ** After the return statement is executed with a semicolon, the statement is executed and the Temporary Variable T2 is parsed. ** after the returnprivatecc () function is executed, ** start to analyze the variables created inside the constructor (excluding the Temporary Variable T1, ** because the Temporary Variable T1 is not created in the returnprivatecc () function, ** it is created in the statement that calls the returnprivatecc () function, ** that is, it is created in cpcls C3 = returnprivatecc, ** only after the statement is executed, the Temporary Variable T1 is parsed ). */Cpcls returnprivatecc () {return cpcls (1);/* If the cpcls replication constructor is private, a compilation error occurs! */} There are only two initialization methods in void test16 () {// C ++: Direct initialization, copy initialization // direct initialization, use a specific constructor to initialize cpcls C1 (1);/* Copy initialization, use the = symbol to initialize ** in G ++, first use 1 to create a temporary object for cpcls, ** then use this temporary object to call the replication constructor to initialize C2 ** although this process will be optimized by G ++ to directly use cpcls (INT) initialize C2 ***** in vs2005, directly use cpcls (INT) to initialize C2 ***** g ++ with the same execution result as vs2005, which is the reason for Compiler optimization, ** if you set the access restriction of the replication constructor to private, G ++ fails to compile */cpcls C2 = 1;/* If the function returns an object instead of a pointer or reference, ** the value following return is initialized by calling the copy constructor. ** if the copy constructor is private, if compilation fails, the return value type of the *** function returnprivatecc () is cpcls, which means that the return value of the function is ** stored in a temporary variable of the cpcls type, then, the cpcls variables created in the function are parsed after the function ** is executed. The temporary cpcls variables are in the cpcls C3 = returnprivatecc () statement (); ** destructor after execution ends */cpcls C3 = returnprivatecc ();}
Summary:
The survival range of the temporary variable is the statement-level semicolon; ending or the braces on the right. After the statement is completed, the temporary variables are parsed ~