Chapter 6 heavy-duty operators and conversions (6)

Source: Internet
Author: User

14.7 auto-increment operators and auto-subtraction Operators
1. Define the auto-increment/auto-increment Operators
The C ++ language does not require the auto-increment or auto-subtraction operators to be class members. However, because these operators change the state of the operation object, they are more inclined to be members.
2. Define the pre-auto increment/pre-auto increment Operators
To be consistent with the built-in type, the prefix operator should return the reference of the increment or decrement object.

Class CheckedArr
{
Public:
CheckedArr (int * B, int * e): beg (B), end (e), cur (B ){}
CheckedArr & operator ++ ()
{
If (end! = Cur)
{
++ Cur;
Return * this;
}
Else
Throw out_of_range ("out of range ");
}
CheckedArr & operator --()
{
If (beg! = Cur)
{
-- Cur;
Return * this;
}
Else
Throw out_of_range ("out of range ");
}
Private:
Int * beg;
Int * end;
Int * cur;
};
3. differentiate operator prefixes and suffixes
The suffix operator function accepts an additional (useless) int-type parameter. This parameter is not required for the normal operation of the suffix operator. Its sole purpose is to distinguish the suffix function from the prefix function.
4. Define suffix Operators
To be consistent with the built-in operator, the suffix operator should return the old value (that is, the value that has not been auto-incrementing or auto-subtracted), and should be returned as a value, rather than a reference.

CheckedArr operator ++ (int)
{
CheckedArr ret (* this );
++ (* This );
Return ret;
}
CheckedArr operator -- (int)
{
CheckedArr ret (* this );
-- (* This );
Return ret;
}
Because these operators are implemented through the prefix version, you do not need to check whether the curr is within the range, that check and the necessary throw are completed in the corresponding prefix operator.
Because int parameters are not used, they are not named.
5. explicitly call prefix Operators
You can call the overload operator explicitly instead of using it as an operator in an expression. If you want to use a function call to call a suffix operator, you must provide an integer real parameter value.
The passed value is usually ignored, but it is necessary to notify the compiler that a suffix version is required.
Generally, it is best to define both prefix and suffix. Defining only prefix or suffix classes will make users accustomed to using two forms feel strange.

From xufei96's column

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.