C++ Primer(第五版) 14.6--14.7节练习

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;
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值