Hey all, this is the OP on an alternative account.
THE PURPOSE
To write an universal iterator class family that will be capable of iterating over std::vectors and T[] arrays, where T = byte = unsigned char.
THE IDEA BEHIND THE ABSTRACT CLASS
The idea was to use the base abstract class for function interfaces so that all functions would have a common type . The following abstract class enforces all children classes to employ the following operators :
** I haven't got a compiler with me atm so please forgive any errors my behalf **
class iterator
{
public :
virtual iterator & operator -- () = 0;
virtual iterator & operator -- (int) = 0;
virtual iterator & operator ++ () = 0;
virtual iterator & operator ++ (int) = 0;
virtual iterator & operator += (int n) = 0;
virtual iterator & operator -= (int n) = 0;
virtual bool operator == (const iterator & o) = 0;
virtual bool operator != (const iterator & o) = 0;
};
So an example derived class would look like this :
/*! \brief ptr iterator can be used to iterate over classic c-style arrays */
class ptr_iterator : public iterator
{
public :
ptr_iterator() : m_p(NULL) {}
ptr_iterator(byte * b) : m_p(b) {}
iterator & operator = (const ptr_iterator &o)
{ m_p = o.m_p; return *this; }
iterator & operator = (byte * b)
{ m_p = b; return *this; }
virtual iterator & operator -- ()
{ --m_p; return *this; }
virtual iterator & operator -- (int)
{ m_p--; return *this; }
virtual iterator & operator ++ ()
{ ++m_p; return *this; }
virtual iterator & operator ++ ()
{ m_p++; return *this; }
virtual iterator & operator += (int n)
{ m_p += n; return *this; }
virtual iterator & operator -= (int n)
{ m_p -= n; return *this; }
virtual bool operator == (const iterator & o)
{ return ((ptr_iterator*)&o)->m_p == m_p; }
virtual bool operator != (const iterator & o)
{ return ((ptr_iterator*)&o)->m_p != m_p; }
private :
byte * m_p;
};
Apart the forementioned iterator we can have an iterator that will iterate over an std::vector
template
class std_iterator : public iterator
{
typedef typename C::iterator c_iterator;
public :
iterator & operator = (const c_iterator & i)
{ m_it = i; return *this; }
iterator & operator = (const std_iterator & o)
{ m_it = o.m_it; return *this; }
virtual iterator & operator ++ ()
{ ++m_it; return *this; }
virtual iterator & operator ++ (int)
{ m_it++; return *this; }
virtual iterator & operator -- ()
{ --m_it; return *this; }
virtual iterator & operator -- (int)
{ m_it--; return *this; }
virtual iterator & operator += (int n)
{ m_it += n; return *this; }
virtual iterator & operator -= (int n)
{ m_it -= n; return *this; }
virtual bool operator == (const iterator &o)
{ return ((std_iterator*)&o)->m_it == m_it; }
virtual bool operator != (const iterator &o)
{ return ((std_iterator*)&o)->m_it != m_it; }
bool operator == (const c_iterator &i)
{ return m_it == i; }
bool operator != (const c_iterator &i)
{ return m_it != i; }
private :
c_iterator m_it;
};
Ok, so basically the forementioned iterators are capable of iterating over an array or a vector.
if we were to use a function that has the following interface :
int DoStuff(iterator &it);
All kinds of iterators would be capable of calling it - and that is the general idea. Let's say that I'm dealing with data coming in a variety of forms - ranging from arrays to vectors :)
Thing is, I need the following functionality :
std_iterator> it = my_vector.begin(); // THIS ONE WORKS
std_iterator> other = it; // THIS ONE WORKS TOO
If I were inside the forementioned function
int DoStuff(iterator &it)
{
iterator a = it + 2; // THIS IS ILLEGAL DUE TO ABSTRACT-NESS OF THE BASE CLASS
// YET I STILL NEED THIS TO WORK. HOW DO I ACHIEVE THAT ?
// DO I SACRIFICE THE ABSTRACT-NESS OF THE BASE CLASS ?
// AT THIS POINT I WAS SERIOUSLY CONSIDERING AN OVERLOAD OF THE
// + OPERATOR BUT HAVE NO IDEA HOW TO DO IT WITH AN ABSTRACT
// CLASS
// (...)
return 0;
}