If the return value of a function is an object, the efficiency of the return statement should be considered. For example
Return string (S1 + S2 );
This is the syntax of a temporary object, indicating "Creating a temporary object and returning it ". Do not think it is different from "create first"
The result of a partial object temp and returning it is equivalent, as shown in figure
String temp (S1 + S2 );
Return temp;
OtherwiseCodeThree things will happen. First, the temp object is created and initialized.
Then the copy constructor copies temp to the external storage unit that saves the returned value. Finally, temp ends at the end of the function.
Is destroyed (calls the destructor ). However, the process of "Creating a temporary object and returning it" is different.
The interpreter directly creates and initializes temporary objects in an external storage unit, saving the copy and structure fee and improving
Efficiency.
Similarly, we do not want
Return int (x + y); // create a temporary variable and return it
Write
Int temp = x + y;
Return temp;
Because the internal data types such as int, float, and double do not have constructor or destructor
The variable syntax does not increase much efficiency,ProgramIt is more concise and easy to read.