14.7. Self-increment operator and self-decrement operator

Source: Internet
Author: User

#include <iostream> #include <string>using namespace std;/*14.7. The increment and decrement operators *///can define a class that points to an array and provides access to the elements in the array. The argument for the/* constructor is two pointers: one to the beginning of the array and the other to the end of the array. The constructor initializes beg and end with these two pointers, and initializes the Curr to point to the first element. */class checkedptr{public:checkedptr (int* a,int* B): Beg (a), end (b), Curr (a) {}//prefix of self-increment checkedptr& operator++ (); checkedptr& operator--();//suffix self-increment checkedptr operator++ (int); checkedptr operator--(int);p rivate:int* beg;int* end;int* curr;};/ * In order to be consistent with the built-in type, the prefix operator should return a reference to the increment or decrement object. This self-increment operator checks Curr based on end to ensure that the user cannot increment the Curr beyond the end of the array. Throws a Out_of_range exception if the Curr increment exceeds end, otherwise curr plus 1 and returns the object reference:*/checkedptr& checkedptr::operator++ () {if (curr==end) Throw Out_of_range ("Beyond the End"); ++curr;return *this;} checkedptr& checkedptr::operator--() {if (Curr==beg) throw Out_of_range ("below the Beg");--curr;return *this;} /* To solve this problem, the postfix operator function accepts an additional (i.e., useless) int parameter. Using the suffix operation identifier, the compiler provides 0 as the argument for the parameter. Although our prefix-type operator function can use this extra parameter, this is not usually the case. That parameter is not required for the normal operation of the postfix operator, and its sole purpose is to differentiate the suffix function from the prefix function. To be consistent with built-in operators, postfix operators should return old values (that is, values that have not been self-increasing or self-reducing), and, instead of returning a reference, it should be returned as a value. */checkedptr checkedptr::operator++ (int) {CHECKEDPTR ret (*this); ++*this;return ret;} Checkedptr checkedptr::operator--(int) {CHECKEDPTR ret (*this);--*this;return ret;} int main () {System ("pause"); return 1;}

14.7. Increment operator and auto decrement operator

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.