6. Differentiate the prefix and Postfix forms of the increment/decrement operators.

Source: Internet
Author: User

In C ++, the front and back of ++ and -- operators can be overloaded. The overload is distinguished by the parameter type. However, no parameter exists in the front or back of ++ or --. to distinguish the two operations, we have to let the back-end have an int independent variable, when it is called, the compiler specifies a 0 value for the int by default.

The following example shows how to perform ++ and -- operations for the char type:

 Class  Char {  Public  : Char (  Char  ); Char & Operator ++ (); //  Frontend type ++      Const Char Operator ++ ( Int ); //  Post-built ++ Char & Operator --(); //  Frontend --      Const Char Operator --( Int ); //  Post-built -- Char & Operator + = ( Int ); // + = Operator  //  ...  } Char C = '  A  '  ; ++ C; //  Call C. Operator ++ (); C ++; //  Call C. Operator ++ (INT ); -- C; //  Call C. Operator --(); C --;//  Call C. Operator -- (INT ); 

Note that the difference between the preceding frontend and post-built return values is described only by the ++ OPERATOR:

The difference between the frontend and posttend of ++ in C is that the frontend is first accumulated and then extracted (increment and fetch), and the latter is first extracted and then accumulated (fetch and increment ). When reload is performed, try not to change the original meaning. Let's look at the implementation of the two operations:

Char & char :: Operator ++ (){( * This ) + = 1 ; //  Increment      Return * This ; // Fetch  }  Const Char CHAR :: Operator ++ ( Int  ) {Char oldvalue = * This ; //  Fetch ++ (* This ); //  Increment      Return  Oldvalue ;} 

From topCodeThe answer is as follows:

Why should we return the reference of the object in front-end mode, because the original object is directly accumulated and the object itself is returned.

Why does the post-built system return a const object? First, the returned value must be an object. This is obvious because the object before the accumulation is returned. Second, why is the const object? What will happen if it is not a const object:

First, it violates the operation syntax rules of the rear ++: the C language does not allow the post ++ operator to be used twice, that is, the following operation is illegal:

 
Int I;
 
I ++;// Error! (But ++ I is legal)

If the returned result is not a const object, the compiler will not report the following error:

 
Char C;
 
C ++;// No error is reported. The call action is as follows:
 
// C. Operator ++ (0). Operator ++ (0 );

Second, the calculation result does not meet our expectation: if a non-const object is allowed to be returned, the calculation result is different from what we expect:

In the code above, the object changed by operator ++ for the second time is the object returned by the First operator ++, rather than the original object. That is, after C ++, the value of C is only added once. This violates our intuition and our intention, so we should be banned!

 

Here, the front and back of the ++ operators should be clear, and the -- operators are similar.

Finally, if you carefully observe the front-end and back-end types, apart from the returned values, the tasks to be completed are the same: Accumulate a value! If we only need to accumulate, it is obvious that the efficiency of using the front-end model is higher than that of the back-end model. There are two reasons:

    • The post-built ++ calls the frontend ++ operation;
    • The post-built system generates a temporary object storage original value, which has the cost of copy construction and structure analysis, but the front-end method does not.

Therefore, use the frontend operation as much as possible!

 

 
References: more than 35 effective methods for programming and design

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.