C++自学笔记_重载运算符_《C++ Primer》
#include <iostream> #include <stdexcept> using namespace std; class CheckedPtr{ public: CheckedPtr(int *b,int *e,int *c): beg(b),end(e),curr(c){ } CheckedPtr(const CheckedPtr &obj): //复制构造函数 beg(obj.beg),end(obj.end),curr(obj.curr){ } CheckedPtr& operator++(); //prefix operators CheckedPtr& operator--(); //prefix operators CheckedPtr& operator++(int); //postfix operators CheckedPtr& operator--(int); //postfix operators int& operator[](const size_t); //重载下标运算符 const int& operator[](const size_t) const; int& operator*(); //重载解解引用操作符 const int& operator*() const; friend bool operator==(const CheckedPtr&,const CheckedPtr&); //重载=运算符 friend bool operator!=(const CheckedPtr&,const CheckedPtr&); //重载!=运算符 friend bool operator<(const CheckedPtr&,const CheckedPtr&); //重载<运算符 friend bool operator>(const CheckedPtr&,const CheckedPtr&); //重载>运算符 friend bool operator<=(const CheckedPtr&,const CheckedPtr&); //重载<=运算符 friend bool operator>=(const CheckedPtr&,const CheckedPtr&); //重载>=运算符 friend CheckedPtr operator+(const CheckedPtr&,const size_t); //重载+运算符 friend CheckedPtr operator-(const CheckedPtr&,const size_t); //重载-运算符 private: int *beg; int *end; int *curr; }; CheckedPtr& CheckedPtr::operator++(){ if(curr==end) throw out_of_range("increment past the end of CheckedPtr"); ++curr; return *this; } CheckedPtr& CheckedPtr::operator--(){ if(curr==beg) throw out_of_range("decrement past the beginning of CheckedPtr"); --curr; return *this; } CheckedPtr& CheckedPtr::operator++(int){ CheckedPtr ret(*this); ++*this; return ret; } CheckedPtr& CheckedPtr::operator--(int){ CheckedPtr ret(*this); --*this; return ret; } /*习题14.23*/ int& CheckedPtr::operator[](const size_t index){ if(index+beg>=end||index+beg<beg) //此处的end指向数组最后一个元素的下一个位置,所以有等号 throw out_of_range("invalid index"); return *(beg+index); } const int& CheckedPtr::operator[](const size_t index) const{ if(index+beg>=end||index+beg<beg) throw out_of_range("invalid index"); return *(beg+index); } int& CheckedPtr::operator*(){ if(curr==end) throw out_of_range("invalid current pointer*"); return *curr; } const int& CheckedPtr::operator*() const{ if(curr==end) throw out_of_range("invalid current pointer*"); return *curr; } /*习题14.25*/ bool operator==(const CheckedPtr &obj1,const CheckedPtr &obj2){ return obj1.beg==obj2.beg&&obj1.end==obj2.end&&obj1.curr==obj2.curr; } bool operator!=(const CheckedPtr &obj1,CheckedPtr &obj2){ return !(obj1==obj2); } bool operator<(const CheckedPtr &obj1,const CheckedPtr &obj2){ return obj1.beg==obj2.beg&&obj1.end==obj2.end&&obj1.curr<obj2.curr; } bool operator>(const CheckedPtr &obj1,const CheckedPtr &obj2){ return obj1.beg==obj2.beg&&obj1.end==obj2.end&&obj1.curr>obj2.curr; } bool operator<=(const CheckedPtr &obj1,const CheckedPtr &obj2){ return !(obj1>obj2); } bool operator>=(const CheckedPtr &obj1,const CheckedPtr &obj2){ return !(obj1<obj2); } /*习题14.26*/ CheckedPtr operator+(const CheckedPtr &obj,const size_t n){ CheckedPtr tmp(obj); if(obj.curr+n>=obj.end) throw out_of_range("too large n"); tmp.curr+=n; return tmp; } CheckedPtr operator-(const CheckedPtr &obj,const size_t n){ CheckedPtr tmp(obj); if(obj.curr-n<obj.beg) throw out_of_range("too large n"); return tmp; }