[英]hasNext() method in array iterator
那是我的通用数组头文件:
#ifndef ARRAY_H
#define ARRAY_H
#include "Number.h"
#include "Iterator.h"
//array is contius memory of numbers
class Array{
protected:
//array has some contius cell of memory each include the data
class Cell{
public:
Number* m_data;
//c'tor:(inline)
Cell(Number* num=NULL): m_data(num){};
//D'tor (inline)
~Cell(){};
};
//composition class of iterator:
class ArrayIterator:public Iterator{
public:
Cell* m_current;
//C'tor:(inline)
ArrayIterator(Cell* cell):m_current(cell){};
//D'tor:
~ArrayIterator(){};
//is there any next numbers
bool hasNext()const;
//is there any prev numbers
bool hasPrev()const;
//returning the current and getforward
Number& next();
//returning the current and getback
Number& prev();
};
Cell* m_head,*m_last;
public:
//C'tor:
Array(const int amount);
//D'tor:
virtual ~Array(){delete[]m_head;};
//Random access operator:
Number& operator [] (const int i)const{return *m_head[i].m_data;};
};
#endif
将Number和Iterator视为抽象类,而Number表示通用数字。
我的问题:如何在ArrayIterator中实现hasNext(),因为ArrayIterator是一个合成类,并且它不“知道”数组的大小
为了实现hasNext()
和hasPrev()
方法, ArrayIterator
需要知道要迭代的当前Array
的边界在哪里。
这可以通过将Cell* m_head,*m_last
与Cell* m_head,*m_last
一起m_current
在ArrayIterator
,或者通过存储指向Array
对象的指针并安排ArrayIterator
可以访问Array
对象的m_head
和m_last
来完成。 无论哪种情况,您都需要在ArrayIterator
存储其他信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.