c++ - hasNext() method in array iterator -


that generic array header file:

#ifndef array_h #define array_h #include "number.h" #include "iterator.h"  //array contius memory of numbers class array{ protected:     //array has contius cell of memory each include 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 next numbers         bool hasnext()const;         //is there prev numbers         bool hasprev()const;          //returning current , getforward         number& next();         //returning current , 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 

consider number , iterator abstract classes , number represent generic number.

my question: how implement hasnext() in arrayiterator, because arrayiterator composition class , dont "know" size of array

to implement hasnext() , hasprev() methods, arrayiterator needs know bounds of current array iterating over.

this can done either storing cell* m_head,*m_last along m_current in arrayiterator, or storing pointer array object , arranging arrayiterator has access m_head , m_last of array object. in either case, need store additional information in arrayiterator.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -