The prefix Suffix of the auto-incrementing auto-subtraction operator. subscripts are used together with reference operations and auto-incrementing auto-subtraction operations.

Source: Internet
Author: User

Original example, Class


[Cpp] // auto-increment and auto-increment operations, prefix suffixes, and suffixes call prefixes! = Call =
# Include "head. h"
// Used to process Arrays
// Start with exercise 14_23 and complete the class. Go to the back of the Code.
Class CheckedPtr {
Public:
CheckedPtr (int * B, int * e): beg (B), end (e), curr (B ){}
Public:
CheckedPtr & operator ++ ();
CheckedPtr & operator --();
// Add a useless form parameter for overloading. In addition, you can use this to explicitly call the suffix operation. Note that the suffix returns the original copy.
CheckedPtr operator ++ (int );
CheckedPtr operator -- (int );

Public:
Int getCurr () {return * curr ;}
Private:
Int * beg;
Int * end;
Int * curr;
};
 
CheckedPtr & CheckedPtr: operator ++ (){
If (curr = end)
Throw std: out_of_range // <stdexcept>. None of them can be used in the past. It can be used this time. (maybe catch or the like won't work, and the block is skipped)
("Increment past the end of CheckedPtr ");
++ Curr;
Return * this;
}
CheckedPtr & CheckedPtr: operator --(){
If (curr = beg)
Throw std: out_of_range
("Decrement past the beginning of CheckedPtr ");
-- Curr;
Return * this;
}
CheckedPtr: operator ++ (int ){
CheckedPtr ret (* this );
+ * This; // prefix operation called
Return ret;
}
CheckedPtr: operator -- (int ){
CheckedPtr ret (* this );
-- * This; // prefix operation called
Return ret;
}
 
Int main (){
Const unsigned size = 10;
Int a [size] = {1, 3, 4, 5, 6, 67, 8, 13, 34 };
CheckedPtr (a, a + size );
Std: cout <ptr. getCurr () <std: endl;
Std: cout <(++ ptr). getCurr () <std: endl;
Std: cout <ptr ++. getCurr () <std: endl;
Std: cout <ptr. getCurr () <std: endl;
Std: cout <(-- ptr). getCurr () <std: endl;
Std: cout <ptr. operator -- (100). getCurr () <std: endl; // explicitly call the suffix. The parameter has no meaning, but must have
Std: cout <ptr. getCurr () <std: endl;
Ptr --;
Ptr --; // out-of-bounds Error


}

Pe14_23.cpp

[Cpp]
<Pre name = "code" class = "cpp"> <strong> // Implementation of the subscript operator:
// Unlike the temporary variables that cannot be referenced when they are returned, the pointer that is created temporarily, and then the reference is unreferenced, the returned variable can be referenced. How can this problem be understood is not very clear, if the parameter is unreferenced, the <span style = "color: # FF0000;"> (mark) </span> </strong>
We found that the csdn code box cannot set the format for itself, and the setting is messy.

[Cpp]
# Include "head. h"
// Reload the subscript operator and the quote operator for this class to make sure That CheckedPtr is valid: it should not dereference or index the elements beyond the end of the array, and test www.2cto.com
Class CheckedPtr {
// Similar code
};
Int & CheckedPtr: operator *(){
If (curr> end | curr <beg)
Throw std: out_of_range
("Out of range ~! ");
Return * curr;
}
Int & CheckedPtr: operator [] (unsigned index ){
If (index> = (end-beg) | index <0)
Throw std: out_of_range
("Out of range ~! ");
Int * temp = curr; // the original location must be protected.
Curr = beg; // reset the curr to make sure that the values are calculated from 0 and the correct subscript is obtained.
For (unsigned I = 0; I <index; I ++)
Curr ++;
Int * ret = curr;
Std: cout <"test \ t * curr:" <* curr <std: endl;
Curr = temp; // restore curr
Std: cout <"test \ t * curr:" <* curr <std: endl;
Return * ret; // both protect the original curr position and restore the original curr before the return, and return the reference for modification. * ret can be returned (the subscript operation returns the reference for modification) requirements
}
Int main (){
// Note the following when performing auto-increment operations: ptr performs curr from the beg passed during initialization, and then stores the memory.
Const unsigned size = 10;
Int a [size] = {1, 3, 4, 5, 6, 67, 8, 13, 34 };
CheckedPtr (a, a + size );

/* Test the impact of subscript access on the curr location. Avoid impact
Std: cout <* ptr <std: endl;
Std: cout <ptr [7] <std: endl;
Std: cout <std: endl <* ptr <std: endl;
Ptr ++;
Std: cout <* ptr <std: endl;
*/
/* Subscript access AND MODIFICATION TEST
For (unsigned I = 0; I <size; I ++ ){
Std: cout <ptr [I] <":";
Ptr [I] = 100;
Std: cout <ptr [I] <"\ t ";

}
*/
// Cross-border Test
// Ptr [-1];
// Ptr [10];

}



From the column huqinweI987
 

Related Article

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.