Arithmetic Operators and Relational operators (1)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.