More effective C + +: self-increase and self-reduction

Source: Internet
Author: User

Long ago (80 's), there was no way to differentiate between + + and--operator prefixes and suffix calls. The problem was complained by programmers, and the C + + language was extended to allow overloading of two forms of increment and decrement operators.

However, there is a syntactic problem where the difference between overloaded functions depends on the difference in their parameter types, but either the prefix of the increment or decrement or the suffix has only one argument. In order to solve this language problem, C + + stipulates that the suffix form has an int type parameter, and when the function is invoked, the compiler passes a value of 0 as the int parameter to the function:

class UPInt { // "unlimited precision int"
public:
 UPInt& operator++(); // ++ 前缀
 const UPInt operator++(int); // ++ 后缀
 UPInt& operator--(); // -- 前缀
 const UPInt operator--(int); // -- 后缀
 UPInt& operator+=(int); // += 操作符,UPInts
 // 与ints 相运算
 ...
};
UPInt i;
++i; // 调用 i.operator++();
i++; // 调用 i.operator++(0);
--i; // 调用 i.operator--();
i--; // 调用 i.operator--(0);

This specification has some quirks, but you'll get used to it. It is particularly noteworthy that these operator prefixes are different from the suffix form return value types. The prefix form returns a reference, and the suffix form returns a const type. Here we will discuss the prefix and suffix forms of the + + operator, which are also used with the-operator.

From the day you started as a C programmer, remember that the prefix form of increment is sometimes called "Add and get back", and the suffix form is called "Retrieve and then Add." These two words are very important because they are the formal specification of the increment prefix and suffix.

// 前缀形式:增加然后取回值
UPInt& UPInt::operator++()
{
 *this += 1; // 增加
 return *this; // 取回值
}
// postfix form: fetch and increment
const UPInt UPInt::operator++(int)
{
 UPInt oldValue = *this; // 取回值
 ++(*this); // 增加
 return oldValue; // 返回被取回的值
}

The suffix operator function does not use its arguments. Its arguments are only used to differentiate between prefixes and suffix function calls. If you don't use parameters in a function, many compilers will display a warning message, which is annoying. To avoid these warnings, a frequently used method omits the name of the parameter you do not want to use, as shown above.

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.