gpt4 book ai didi

c++ - C++入门5版。 StrBlobPtr减量运算符

转载 作者:行者123 更新时间:2023-12-01 14:43:08 32 4
gpt4 key购买 nike

这是错字吗?

在C++入门5中。 ed页面702:

StrBlobPtr& StrBlobPtr::operator--()
{
// if curr is zero, decrementing it will yield an invalid subscript
--curr; // move the current state back one element
check(-1, "decrement past begin of StrBlobPtr");
return *this;
}

因此,如您所见,传递 -1进行检查将导致它始终抛出。他的意思是 check(curr, "decrement past begin of StrBlobPtr");吗?

以下是如何实现 check的代码段:
std::shared_ptr<std::vector<std::string>>
StrBlobPtr::check(size_type pos, const std::string &msg) const {
auto ret = wptr.lock();
if (!ret)
throw std::runtime_error("unbound StrBlobPtr");
if (pos >= ret->size())
throw std::out_of_range(msg);
return ret;
}

最佳答案

我已经检查了我的C++ Primer 6th Edition,并且以下代码出现在第567页上:

StrBlobPtr& StrBlobPtr::operator--()
{
// if curr is zero, decrementing it will yield an invalid subscript
--curr; // move the current state back one element
check(curr, "decrement past begin of StrBlobPtr");
return *this;
}

StrBlobPtr的实现显然已经改变了很多,但这足以确认 -1是一个错字并已在第六版中得到修复。

实际上,这是在 C++ Primer Errata上提到的:

  • Page 567: In the StrBlobPtr::operator--() function, the call to check should pass curr as the first argument, not -1.

关于c++ - C++入门5版。 StrBlobPtr减量运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029018/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com