Efficiency problems of C + + overloaded self-increment operators

Source: Internet
Author: User

C + + Specifies that the suffix form has an int type parameter, and when the function is called, the compiler passes a value of 0 as the int parameter to the function.

The prefix form of the increment means "add and then retrieve", and the suffix form means "fetch and then increment".

1#include"stdafx.h"2#include"assert.h"3     classA4     {5      Public:6Ainti)7 : M_i (i)8         {9         }Ten         //++i Onea&operator++() A         { -++m_i; -             return* This; the         } -         //i++ -         ConstAoperator++(int) -         { +A tmp = * This; -++(* This);//implemented by prefix increment +             returnA (TMP); A         } at         intm_i; -     }; -     int_tmain (intARGC, _tchar*argv[]) -     { -         inti =0; -         intI1 = i++;//i1 = 0; i = 1; in         intI2 = ++i;//i2 = 2; i = 1; -A A (0); toA A1 = a++;//i1 = 0; i = 1; +A A2 = ++a;//i2 = 2; i = 1; -          the         //a++++;//Avoid *         //++++a;// Support $ASSERT (I1 = =a1.m_i);Panax NotoginsengASSERT (I2 = =a2.m_i); -         return 0; the}

Description

1. The + + operation symbol overload in the class must be guaranteed to be the same as global + +.

2. In order to differentiate before and after, use + + () to indicate the former self-increment, with + + (int) after self-increment.

3. Since the "++++a" syntax should be supported as defined by the previous self-increment, and two pre-increment should be the self-operation of the A object, if a type is returned, then the second self-increment call is the pre-increment of the temporary object.
4. Post-increment should return "Const Complex". This prevents the use of shapes such as "a++++".

5. The post-increment operator function is generally implemented by the previous self-increment operation.

As can be seen from the above, the post-operation needs to copy a useless temporary object, the efficiency must be lower.

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.