14.27
/* 前置递增 */
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)
{
auto temp = *this;
++*this;
return temp;
}
/* 后置递减 */
StrBlobPtr StrBlobPtr::operator--(int)
{
auto temp = *this;
--*this;
return temp;
}
14.28
StrBlobPtr& StrBlobPtr::operator+=(const size_t &n)
{
check(curr, "increment past end of StrBlobPtr");
curr += n;
return *this;
}
StrBlobPtr& StrBlobPtr::operator-=(const size_t &n)
{
curr -= n;
check(curr, "decrement past begin of StrBlobPtr");
return *this;
}
StrBlobPtr operator+(const StrBlobPtr &lhs, const size_t &n)
{
auto ret = lhs;
ret += n;
return ret;
}
StrBlobPtr operator-(const StrBlobPtr &lhs, const size_t &n)
{
auto ret = lhs;
ret -= n;
return ret;
}
14.29 递增和递减运算符会改变对象本身,所以不能是const的。
14.30
string& operator*() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
string* operator->() const
{
return &this->operator*();
}
const string& operator*() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
const string* operator->() const
{
return &this->operator*();
}
14.31 StrBlobPtr类的数据成员类型是weak_ptr<vector<string>>和size_t,weak_ptr定义了自己的拷贝构造函数,赋值运算符和析构函数,size_t是内置类型,所以StrBlobPtr可以使用合成的拷贝构造函数,赋值运算符和析构函数。
14.32
class TestPtr {
public:
string* operator->()
{
return ptr->operator->();
}
private:
StrBlobPtr *ptr;
};