Recommended mode: Use + = instead of + to save copying objects
Exercise 14.12 is called with ++ =. The disadvantage is that it is copied many times, but why is it wrong? Isn't this referenced like RHS? Because it is a member function,Where is this wrong?
Inlinesales_item & sales_item: Operator + = (const sales_item & RHs) {sales_item item; item = * This + RHS; // pe14_12.cpp: 56: 17: Error: no match for 'operator + 'in' * (sales_item *) This + RHS '* This = item; return * This;} sales_itemoperator + (const sales_item & LHS, const sales_item & RHs) {// operator + need to call operator + = sales_item RET (LHS); RET + = RHS; return ret ;}
According to the original EXAMPLE Class
= And! = Matching appearance, and! = Code return is greatly simplified by calling =! (LHS = RHs );
Note the const attribute of the member function. This has high requirements on the function nature (eg. same_isbn ())
Review: inline is an optimization that does not have to be used in combination with the class and can be used outside.