Continue to organize the remainder of the sixth chapter on the provisional objects of a few guidelines.
1. In some cases, the compiler can produce the necessary, or convenient, temporary objects, which are defined by the compiler . For example, for the following actions:
T A, B;
T C = A + b;//t operator+ (const t&, const t&)
A. The compiler produces a temporary object, places the results of the a+b, and then uses the copy constructor of T to treat the temporary object as the initial value of C.
B. Another possible way is to place the A+b value in C directly in the form of a copy, which is similar to the code: T C (A + B), thus avoiding the structure and composition of the temporary object.
C. In addition, if RVO (return value optimization) is in effect, you can find the result of the expression directly in C and no longer require a copy construct.
For the above three ways C + + is not a clear standard, but to leave a certain degree of freedom.
Because of the competition in the compiler market, many compilers do not produce temporary objects on the above expressions, but for assignment construction expressions, it is not possible to avoid temporary objects, so t C = a + B is better than t C; c = A + B efficient:
T A, B;
T C;
c = a + B;
2.
The destruction of a temporary object should be the last step in the full expression evaluation process, which causes the temporary object to be generated。 That is, if an expression causes a temporary object to be generated, then the temporary object needs to be completely executed before the expression is destroyed, for example:
((Obja > 1024) && (OBJB > 1024))? Obja + Objb:foo (Obja, OBJB);
A total of five expressions, in a question mark expression, any one of the temporary objects produced by any subexpression should be destroyed after the complete expression is evaluated.
3. Any temporary object that contains the result of an expression execution should be retained until the initialization of object is complete , for example:
T C = isTrue? 0:a + b;
The temporary objects produced by a+b must be retained until C is initialized.
4. If a temporary object is bound to a reference (reference), the temporary object needs to be persisted to the end of the referenced object initialized by it, or to the end of the life cycle of the temporary object, depending on which case occurs first. For example:
Const std::string& space = "";
will produce the code for this program:
std::string temp;
Temp.std::string::string ("");
Const std::string& Space = temp;
The life cycle of the T EMP must be maintained during space activity.
The sixth chapter of the content is finished, and then will continue to organize the seventh chapter is also the book's last chapter of the content.