C++ Primer(第5版) 练习 14.27
练习 14.27 为你的StrBlobPtr类添加递增和递减运算符。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
class StrBlobPtr{public:StrBlobPtr(): curr(0) {}StrBlobPtr(StrBlob &s, size_t sz = 0): wptr(s.data), curr(sz) {}string &deref() const;StrBlobPtr &incr();StrBlobPtr& operator++();StrBlobPtr& operator--();StrBlobPtr& operator++(int);StrBlobPtr& operator--(int);private:shared_ptr<vector<string>> check(size_t, const string &) const;weak_ptr<vector<string>> wptr;size_t curr;
};StrBlobPtr& StrBlobPtr::operator++(){check(curr, "increment past end of StrBlobPtr");++curr;return *this;
}
StrBlobPtr& StrBlobPtr::operator--(){--curr;check(curr, "decrement past begin of StrBlobPtr");return *this;
}
StrBlobPtr& StrBlobPtr::operator++(int){StrBlobPtr ret = *this;++*this;return ret;
}
StrBlobPtr& StrBlobPtr::operator--(int){StrBlobPtr ret = *this;--*this;return ret;
}