9 Class operations
This chapter covers
- Choosing between operators and methods
- Handling self-assignment and return value optimization
- Cooperating with the compiler for class members
- Deciding between explicit and implicit conversions
- Prefix and postfix versions of increment and decrement operators
This chapter continues the discussion of how to use instances well. The concept of the class invariant is always in view, but these mistakes need to be addressed with a different focus than the previous chapters. This certainly does not mean that it is less important, only that the emphasis is broader, touching on areas that do not necessarily directly affect the state of an object.
The category of performance is frequently addressed in these mistakes, with several emphasizing this aspect by focusing on eliminating unnecessary temporary objects. These temporary objects occur when an intermediate step in an expression evaluation requires an intermediary object to hold the results of a partial evaluation, which will be used in further evaluation. Knowing when these temporaries are created and how to design a class to eliminate many of them significantly affects the number of constructor and destructor calls.
Other mistakes focus on misusing operators that affect common usage or performance. C++ provides a significant amount of flexibility for the developer, and this flexibility must be respected to avoid using features improperly.