The difference between the prefix and suffix of the self-increment and decrement operators of C + +

Source: Internet
Author: User

Believe that a lot of people have just started to learn programming, but also for this problem, the prefix and suffix, in a long time ago, there is no way to distinguish between + + and-operator prefix and suffix of the call. However, the C + + has been expanded.

However, regardless of the prefix or suffix, there is only one parameter. To solve this problem, C + + specifies that the suffix form has an int type parameter, and when the function is called, the compiler passes a 0 as an int parameter to the function:

class Upint {

Public

upint& operator++ (); + + Prefix

Const upint operator++ (int); + + suffix

upint& operator--(); --Prefix

Const upint operator--(int); --suffix


.......

}

Upint i;

++i; Call i.operator++ ();

i++; Call i.operator++ (0);

I.; Call i.operator--();

i--; Call i.operator--(0);


It is important to note that these operator prefixes are different from the return value types in the suffix form. returns a reference in the form of a prefix that returns a const type in the suffix form .

Distinguishing between prefixes and suffixes is also very simple, just remember a word: the prefix form of increment is called " increment and then take value ", the suffix form is called " fetch and then increment ".

prefix form

upint& upint::operator++ () {

*this + = 1; Increase

return *this; Retrieving values

}

suffix Form

Const upint upint::operator++ (int) {

Upint oldValue = *this; Retrieving values

+ + (*this); Increase

return oldValue; Returns the retrieved value

}


The postfix operator function does not use its function, and its arguments are only used to differentiate between prefixes and suffix functions. If you are concerned about efficiency, you may have found that the postfix operator function must establish a temporary object as its return value. This temporary object must be constructed and at the end is a destructor. The prefix operator does not have such a temporary object. It can be concluded that, if only for efficiency, use the prefix operator as much as possible, with fewer suffix operators, unless the suffix operator must be used.

As you can see, mastering the prefixes and suffixes of increment and decrement is easy. It is sufficient to understand their correct return value types and the rules that postfix operators should implement based on the prefix operator.



This article is from the "Jimmy http://jimmystar.blog.51cto.com/3716199/1584964 C + +" blog, make sure to keep this source.

The difference between the prefix and suffix of the self-increment and decrement operators of C + +

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.