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 + +