我正在编写我的 Arrayhandler 类,我需要重载operator+
和operator++
.
operator++
超载是个好主意operator+(1)
吗?我得到一个无限循环,因为据我所知_current = _pointee + i
(size_t 在哪里i
)没有改变_current
。为什么?以这种方式添加指针是否正确?
class ArrayHandler
{
private:
size_t _size;
Pointee *_pointee;
Pointee *_end;
mutable Pointee *_current;
....
}
我的托儿:
template <typename Pointee>
ArrayHandler<Pointee>::ArrayHandler(size_t size):
_size(size), _pointee(new Pointee[_size]), _end(_pointee+_size), _current(_pointee)
{};
运算符+:
ArrayHandler<Pointee>& ArrayHandler<Pointee>::operator+(size_t i)
{
if (!defined())
throw MisUse(undefArray, 0);
if ( _pointee + i > _end || _pointee + i < _pointee)
throw MisUse(badIndex, i);
_current = _pointee + i;
return *this;
};
运算符++:
template <typename Pointee>
ArrayHandler<Pointee>& ArrayHandler<Pointee>::operator++()
{
if (!defined())
throw MisUse(undefArray, 0);
if ( stop() )
throw MisUse(badIndex, 0);
++_current;*/
this->operator+(1);
return *this;
};
导致无限执行的while循环:
while (!ar3.stop())
{
++ar3;
++count;
}
和stop()
方法:
bool stop() const {return _current ==_end;}
更新:
无限while循环的原因是我通过operator+实现了operator++,在我的例子中,它确实改变了_current,每次start+1,所以在第二次迭代之后我的_current保持不变。每次都被start+1重复RESET。
伙计们!!