Introduction to C ++ the difference between front ++ and back ++

Source: Internet
Author: User

Just touchedC ++New students are often confused by front ++ and back ++. This concept is very important in C ++. It is necessary to understand the front ++ and back ++. The following describes the differences between front ++ and back ++.

Front ++:

 
 
  1. type operator++); 

Rear ++:

 
 
  1. const type operator++int ); 

In order for the compiler to distinguish between front and back ++, C ++ requires that the suffix form has an int type parameter. When a function is called, the compiler passes a 0 value as the int parameter to the function. Otherwise, it cannot be distinguished because only the object itself is an input parameter.

The following is a simple example:

 
 
  1. Class CInt {
  2. Private:
  3. Int m_value;
  4. //
  5. };
  6. CInt & CInt: operator ++ () // There is no parameter in front of it and a reference is returned.
  7. {
  8. This-> m_value + = 1;
  9. Return * this;
  10. }
  11. Const CInt: peartor ++ (Int) // There is an anonymous parameter at the end and the const value is returned.
  12. {
  13. CInt old = * this;
  14. ++ (* This );
  15. Return old;
  16. }

The preceding implementation explains a key problem: the prefix is more efficient than the Postfix. the Postfix needs to construct a temporary object and return it.

So why are the front and back return parameters different?

The frontend only performs operations on itself and returns itself, so that you can directly operate on the returned object, as shown in figure

 
 
  1. ++it)->function) 

When the returned result is not the original object, you can perform additional operations to change the status of the temporary object.

So why not return const? Because you cannot do this, the returned reference will be invalid and the temporary object will no longer exist.

Therefore, if a const object is returned in the backend, the temporary object is limited to incorrect operations, and the caller is explicitly notified that the object is only a copy of the original object.

Another reason is that the built-in int type does not support I ++. If the post ++ returns a modifiable copy, it will be different from the built-in int type. Therefore, modification to the return value should be prohibited.

We hope that the above content will help you.

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.