Self-defined self-subtraction subscript solution reference and so on--around the Checkedptr class continued 14.26 Questions

Source: Internet
Author: User

Optimization of Pe14_26_2.cpp//dui 26 questions

The appropriate const protection, and the order in which the operator functions are defined, is best followed by the declaration order, which looks a little clearer

#include "head.h"//Version two, optimize the addition and subtraction of the problem, I want to add a negative situation directly into the call to subtraction, whereas the opposite//call the opposite operator when passing parameters to note that the incoming (0-change) has a transformation class checkedptr{
	Public:checkedptr (int* B, int* e): Beg (b), End (e), Curr (b) {} public:checkedptr& operator++ ();
	checkedptr& operator--();
	Add a useless parameter to implement the overload, in addition, you can use this explicit invocation suffix operation, note that the suffix returns the original copy of Checkedptr operator++ (int);
	
	Checkedptr operator--(int);
	int& operator* ();
	
	int& operator[] (unsigned);
	BOOL operator< (const checkedptr&)//Add these operations to the class: refers to the member function.
	BOOL operator== (const checkedptr&);
	Add and subtract numbers to adjust pointer point, return a copy, point to new position 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> that's not going to work, this time.
	, (maybe catch or not, The Block 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 checkedptr::operator++ (int) {CHECKEDPTR ret (*this);	
++*this;//called the prefix operation return ret;
	} checkedptr checkedptr::operator--(int) {CHECKEDPTR ret (*this);	
--*this;//called the prefix operation return ret; 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;//needs to be protected from the original position curr = beg;//Reset Curr, guaranteed to be calculated from 0 each time, get the correct subscript 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;//to protect the original Curr position, restore before returning, but also returned to the reference for modification, I do not think I can do ... I did it~!!!!!!!!!!!!!!! 1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1} bool Checkedptr::operator< (const checkedptr& RHS) {if (Curr-beg) < (Rhs.curr-rhs.beg)) return 1;
return 0;
	BOOL checkedptr::operator== (const checkedptr& RHS) {if (Curr-beg) = = (Rhs.curr-rhs.beg)) return 1;	
return 0;
}//Two What is the significance of the pointer addition//(Lhs.curr-lhs.beg) + (Rhs.curr-rhs.beg)-Lhs.beg; It's too much trouble.//int operator+ (const checkedptr& LHS, const checkedptr& RHS) {/* pointer subtraction, return difference, can be positive or negative, provided that the same array int operator-(CO 
NST checkedptr& LHS, const checkedptr& RHS) {return (Lhs.curr-rhs.curr);//private, also defined as member} * meaning is not significant, or use pointers to add and subtract numbers useful */checkedptr checkedptr::operator+ (const int change) {//unrestricted plus sign will be troublesome, only to satisfy PTR + (-10); This operation if (change < 0) {CHECKEDP
		TR ret = this->operator-(0-change);
	return ret; else{if (Curr + change) < Beg | |
		(Curr + change) >= end)//also judge out of bounds throw std::out_of_range ("out of range");
		CHECKEDPTR ret (beg, end); Ret.curr = curr;//Necessary: otherwise addition does not have cumulative, PTR + 1; PTR + 2; Total +2 if (change >= 0) for (int i = 0; i < change; i++) ret++;
	return ret; } checkedptr checkedptr::operator-(const int change) {//unrestricted plus sign will be troublesome, only to satisfy PTR-(-10); This action if (change < 0) {CHECKEDP
		TR ret = this->operator+ (0-change);
	return ret; else{if ((Curr-change) < Beg | |
		(Curr-change) >= end)//Also to judge out of bounds throw std::out_of_range ("out of range");
		CHECKEDPTR ret (beg, end); Ret.curr = curr;//Necessary: otherwise addition does not have cumulative, PTR + 1;
		PTR + 2; A total of +2 for (int i = 0; i < change; i++) ret--;
	return ret;
	int main () {const unsigned size = 10;
	int A[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	Checkedptr Ptr1 (A, a + size);
	Checkedptr Ptr2 (A, a + size);
	Test addition Std::cout << *ptr2 << Std::endl;
	PTR2 = ptr2 + 3;
	Std::cout << "after PTR2 + 3:" << *ptr2 << Std::endl;
	PTR2 = ptr2 + 4;
	Std::cout << "after PTR2 + 4:" << *ptr2 << Std::endl;  PTR2 = Ptr2 + (-1);/plus negative, another idea is to use judgment conditions, if there are negative, direct call subtraction operator, to be added (modified) std::cout << "after Ptr2 + ( -1):" << *ptr2 <&lT
	Std::endl;
	PTR2 = ptr2-1;
	Std::cout << "after ptr2-1:" << *ptr2 << Std::endl;  PTR2 = PTR2-(-1);//lighten the number, another way is to use judgment conditions, if there are reducing number, directly call the addition operator, to be added (modified) std::cout << "After Ptr2-( -1):" << *ptr2
	<< Std::endl;
 PTR2-(-2);/the upper bound//ptr2 = ptr1 + 11;//test out of Bounds//ptr}


14.24

Subscript and reconciliation references need to check the elements before the beginning of the array, subscript no, I preach unsigned, starting from 0, the solution to the reference is not necessary, because the entire class object has been Curr the Cross-border check, so the object can not have a hyper-boundary state.

14.27

Allow constructors to pass in array arguments, the disadvantage being boundary judgment, compared to (beg, end), the incoming array has no end, but you can use size () to judge yourself (bad state, throw it)


14.28 does not define the const version of the self-amplification and decrement operators, why

Because to change the member Curr, different from the general meaning of the pointer self-reduction

14.29

Why don't I implement the arrow operator

Afraid of understanding inconvenience bar, the need for the arrow operation is to take an array of elements bar, the arrow operator can also be a member of the object, causing misunderstanding

14.30 define a checkedptr version, save the screen array, and implement overloaded self-subtraction, dereference, arrows, and so on for the class.



#include "head.h"//Save the screen array and define various operations class screen{Public:typedef Std::string::size_type index;//leverages the typedef char GET (
		) const{//return Contents[cursor];
	Return ' T ';//test}//overloaded void settest (Index i = 1) {cursor = i;}
	Index Gettest () {return cursor;} inline char get (index HT, index WD) const;//explicitly define the inline index get_cursor () const;//the latter is easy to read, and the declaration in the definition of the class is more clear Public:screen (in
	Dex HT, index WD): Height (HT), Width (wd) {} screen (): Height (0), Width (0) {} private:std::string contents;
	

Index cursor;//cursor Index height, width;//simultaneous declaration of multiple}; Class checkedptr{Public:checkedptr (screen* B, screen* e): Beg (b), End (e), Curr (b) {} public:checkedptr& operator+
	+();
	checkedptr& operator--();
	Add a useless parameter to implement the overload, in addition, you can use this explicit invocation suffix operation, note that the suffix returns the original copy of Checkedptr operator++ (int);
	
	Checkedptr operator--(int);
	screen& operator* ();
	
	screen& operator[] (unsigned);
	BOOL operator< (const checkedptr&)//Add these operations to the class: refers to the member function.
	BOOL operator== (const checkedptr&); Add and subtract numbers to adjust pointer direction, returnA copy pointing to the new location checkedptr operator+ (int);
	
Checkedptr operator-(int);
	Public:screen Getcurr () {return *curr;} private:screen* Beg;
	Screen* end;
screen* Curr;

}; checkedptr& checkedptr::operator++ () {if (Curr = end) throw std::out_of_range//<stdexcept> that's not going to work, this time.
	, (maybe catch or not, The Block skipped) ("increment past the end of Checkedptr");
	++curr;
return *this; } checkedptr& checkedptr::operator--() {if (Curr = = Beg) throw Std::out_of_range ("decrement Past" beginning
	of Checkedptr ");
	--curr;
return *this;
	} checkedptr checkedptr::operator++ (int) {CHECKEDPTR ret (*this);	
++*this;//called the prefix operation return ret;
	} checkedptr checkedptr::operator--(int) {CHECKEDPTR ret (*this);	
--*this;//called the prefix operation return ret; } screen& checkedptr::operator* () {if (Curr > End | | Curr < BEG) throw Std::out_of_range ("out of range~!
	");
return *curr; } screen& checkedptr::operator[] (unsigned index) {if (index >= (End-beg) | | Index < 0) throw std::out_of_range ("Out of range~!"); Screen * temp = curr;//needs to be protected from the original location Curr = beg;//Reset Curr, guaranteed to be calculated from 0 each time, get the correct subscript for (unsigned i = 0; i < index; i++) curr+
	+;
	screen* ret = Curr;
	Std::cout << "Test\t*curr:" << curr->gettest () << Std::endl;
	Curr = temp;//Restore Curr std::cout << "Test\t*curr:" << curr->gettest () << Std::endl;
return *ret;
	BOOL checkedptr::operator< (const checkedptr& RHS) {if (Curr-beg) < (Rhs.curr-rhs.beg)) return 1;
return 0;
	BOOL checkedptr::operator== (const checkedptr& RHS) {if (Curr-beg) = = (Rhs.curr-rhs.beg)) return 1;	
return 0;
}//Two What is the significance of the pointer addition//(Lhs.curr-lhs.beg) + (Rhs.curr-rhs.beg)-Lhs.beg; It's too much trouble.//int operator+ (const checkedptr& LHS, const checkedptr& RHS) {/* pointer subtraction, return difference, can be positive or negative, provided that the same array int operator-(CO 
NST checkedptr& LHS, const checkedptr& RHS) {return (Lhs.curr-rhs.curr);//private, also defined as member} * meaning is not significant, or use pointers to add and subtract numbers useful * * checkedptr checkedptr::operator+ (const int Change) {//unrestricted sign will be more troublesome, only to meet PTR + (-10); This operation if (change < 0) {checkedptr ret = this->operator-(0-change);
	return ret; else{if (Curr + change) < Beg | |
		(Curr + change) >= end)//also judge out of bounds throw std::out_of_range ("out of range");
		CHECKEDPTR ret (beg, end); Ret.curr = curr;//Necessary: otherwise addition does not have cumulative, PTR + 1;
		PTR + 2; A total of +2 if (change >= 0) for (int i = 0; i < change; i++) ret++;
	return ret; } checkedptr checkedptr::operator-(const int change) {//unrestricted plus sign will be troublesome, only to satisfy PTR-(-10); This action if (change < 0) {CHECKEDP
		TR ret = this->operator+ (0-change);
	return ret; else{if ((Curr-change) < Beg | |
		(Curr-change) >= end)//Also to judge out of bounds throw std::out_of_range ("out of range");
		CHECKEDPTR ret (beg, end); Ret.curr = curr;//Necessary: otherwise addition does not have cumulative, PTR + 1;
		PTR + 2; A total of +2 for (int i = 0; i < change; i++) ret--;
	return ret;
	int main () {const unsigned size = 10;
	
	Screen S[size];

	S[1].settest (8); Std::cout << s[1].gettest ()<< Std::endl;
	Checkedptr PTR (s, S + size-1);
	Std::cout << (*++ptr). Gettest () << Std::endl;
	Std::cout << (*++ptr). Gettest () << Std::endl;
	
	
Std::cout << ptr[5].gettest () << Std::endl;
 }















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.