3. Using the C++ Compiler Options
6. Creating and Using Templates
9. Improving Program Performance
9.4.1 Choosing to Pass Classes Directly
9.4.2 Passing Classes Directly on Various Processors
10. Building Multithreaded Programs
12. Using The C++ Standard Library
13. Using the Classic iostream Library
C++ functions often produce implicit temporary objects, each of which must be created and destroyed. For non-trivial classes, the creation and destruction of temporary objects can be expensive in terms of processing time and memory usage. The C++ compiler does eliminate some temporary objects, but it cannot eliminate all of them.
Write functions to minimize the number of temporary objects as long as your programs remain comprehensible. Techniques include using explicit variables rather than implicit temporary objects and using reference parameters rather than value parameters. Another technique is to implement and use operations such as += rather than implementing and using only + and =. For example, the first line below introduces a temporary object for the result of a + b, while the second line does not.
T x = a + b; T x(a); x += b;