C ++ Articles: Guru of The Week # 2 - Temporary object

zhaozj2021-02-08  227

Author: Hub Sutter Translator: plpliuly

/ * This article is the second copy of the translator for self-entertainment translation. The original copyright is a Hub Sutter (the famous C expert, "Exceptional C " author). The translation of this article did not have the consent of the original author, only for learning discussion. - Translator * /

# 2 Temporary object difficulty: 5/10

Unnecessary temporary objects often lead to low code redundancy and execution efficiency.

Question: Suppose you are watching a code, there is a function in the code. There is at least 3 in this function produce unnecessary temporary objects. See how you can find a few, how to modify it? String Findaddr (List L, String Name) {for (List :: Iterator i = L.BEGIN (); i! = L.End (); i ) {if (* i == Name) {RETURN (* i) .addr;}} return ";

Answer: No matter what you believe, there is three obvious unnecessary temporary objects in this few lines of code, and there are less obvious temporary objects, there is also a place worth noting. String Findaddr (List L, String Name) ^^^^^^^^^^^^^^^^^^^^^ 2 ^^^^^ 1 & 2. The parameter should be a const reference type. The above statement The transmitted value parameters cause List and String copy operations, which may be time consuming. [Guidelines] Try to use Const & to replace the value copy transfer parameters. For (list :: item i = l.begin (); i! = L.end (); i ) ^ 3 ^ 3. Here is slightly difficult than the top. Here, if you use before using the self-increment operation, it will be more efficient, because the object's rear self-increment operation requires an object to perform the self-increment operation and return a temporary object of the original value of the original value (the translator: overloaded The readers of self-increasing and post-in-time operators should be clearly different from the differences). Note that this is also applicable for C original types like INT. [Guidelines] Try to use it before use, avoid it from using it. IF (* i == name) ^ 4 4. Although we didn't see the definition of the Employee class, the above statement must be valid, this class must define the String type conversion operator or define the String type as parameters. Constructor. Both of these cases produce a temporary object, the former calls the String equivalent comparison operator (Operator ==), and the latter calls the equivalent comparison operator of Employee. (The only thing that does not produce a temporary object is that the String and the Employee class are overloaded with equal comparison operators with each other as parameter types.) [Guidelines] To be careful to hide the temporary objects generated after the parameter conversion. A solution to avoiding this temporary object is to limit the constructor with an ExPlicit modifier. Return ""; ^ 5 5. A temporary space String object is generated here. A better way is to declare a partial String object to store the return value, and finally the statement returns to the unified return with a statement that returns the value of the object. This will make the compiler can use the return value optimization means to omit this partial object in some cases. For example, the following scenario: The caller calls this function with the following code: String a = FindAddr (1, "Harold"); [Guidelines] Follow the principle of a single export. Never exist multiple returns in the same function. [Note: After making more performance tests, I don't fully agree with the principles described above. This has been made in "Exception C ". ]

转载请注明原文地址:https://www.9cbs.com/read-1057.html

New Post(0)