template <class TVal> class vector { private: std::vector<TVal> m_container; typename std::vector<TVal>::iterator m_item; public: vector ( void ) {m_item = m_container.begin();} virtual ~vector ( void ) {} inline void move_first() { m_item = m_container.begin(); } inline void move_last() { m_item = --m_container.end(); } inline bool move_next() { ++m_item; return !eof();} inline bool operator ++() { return move_next(); } inline bool move_previous() { --m_item; return !bof(); } inline bool bof() { return (m_container.begin()==m_item); } inline bool eof() { return (m_container.end()==m_item); } inline void value(const TVal& val) { *m_item = val; } inline TVal& value() { return *m_item; } inline void insert_front(const TVal& val) { m_container.push_front(val);} inline void insert_back(const TVal& val) { m_container.push_back(val);} inline bool erase_current() { return m_container.erase(m_item); } inline void erase_last() { m_container.pop_back(); } inline void clear() { m_container.clear(); } inline size_t size() { return m_container.size(); } inline void reserve(size_t size) { m_container.reserve(size); } inline void resize(size_t size) { m_container.resize(size); } };
I have an instance of this class with 3 elements (boost::shared_ptr's) in it.
When I try to iterate it with a while loop, at the second iteration (that'd be the second element starting from the end) I'm getting a null pointer exception.
my_vector.move_last(); while (!my_vector.bof()) { std::cout << my_vector.value() <<std::endl; if (my_vector.value()->handle_mouse(e)) { handled = true; break; } my_vector.move_previous(); }
If I iterate it fowards, it works ok.
my_vector.move_first(); while (!my_vector.eof()) { if (my_vector.value()->handle_mouse(e)) { handled = true; break; } my_vector.move_next(); }
Could you throw me a hand at realizing what I am doing wrong?
Thanks.