More effective C + + clause 6 distinguishes the increment/decrement operator from the front (prefix) and post (postfix) forms

Source: Internet
Author: User

1. Since the pre-increment and post-increment operators are unary operators, the overloads are distinguished by adding an int parameter (dummy parameter) to the post-increment, and when the post-increment is called, the compiler automatically assigns a 0 value to the parameter.

2. The pre-increment operator returns a reference to the object that called it, and the increment operator returns a const temporary object

3. Since the former self-increment is a reference to the original object, it is possible to perform a formula similar to ++++a;

However, the post-increment returns a copy of the object before the increment operator after the call, and if the temporary object returned by the increment operator is not const, then the a++++ phenomenon may occur, and the second + + function is actually a temporary object, which is obviously out of the original intention. One of the principles in C + + is "Never let a user change a temporary object" (except exception handling), because the temporary object is generated by the compiler, we cannot actively use it, and its lifetime can not be mastered by us, so in addition to using temporary objects to act as a "vector" of incoming parameters or return values, Any changes to the temporary object are meaningless and are severely forbidden by the compiler.

The same goes for the inability to pass a temporary object to a function that accepts a non-const reference. That is, unless a reference is returned, the temporary object is returned, and any change to the temporary object (except exception handling) is not allowed!

4. Since the increment to create an object to save a copy of the previous object, it is lower than the previous self-increment, in the case of the same effect is recommended before the use of self-increment.

5. If both pre-and post-increment are to be defined, the definition of post-increment is best achieved by the previous self-increment as the basis for implementation, thus guaranteeing the consistency of their behaviour.

More effective C + + clause 6 distinguishes the increment/decrement operator from the front (prefix) and post (postfix) forms

Related Article

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.