Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hey all, this is the OP on an alternative account.</p> <p><strong>THE PURPOSE</strong> To write an universal iterator class family that will be capable of iterating over std::vectors and T[] arrays, where T = byte = unsigned char.</p> <p><strong>THE IDEA BEHIND THE ABSTRACT CLASS</strong></p> <p>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 :</p> <p>** I haven't got a compiler with me atm so please forgive any errors my behalf **</p> <pre><code>class iterator { public : virtual iterator &amp; operator -- () = 0; virtual iterator &amp; operator -- (int) = 0; virtual iterator &amp; operator ++ () = 0; virtual iterator &amp; operator ++ (int) = 0; virtual iterator &amp; operator += (int n) = 0; virtual iterator &amp; operator -= (int n) = 0; virtual bool operator == (const iterator &amp; o) = 0; virtual bool operator != (const iterator &amp; o) = 0; }; </code></pre> <p>So an example derived class would look like this :</p> <pre><code>/*! \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 &amp; operator = (const ptr_iterator &amp;o) { m_p = o.m_p; return *this; } iterator &amp; operator = (byte * b) { m_p = b; return *this; } virtual iterator &amp; operator -- () { --m_p; return *this; } virtual iterator &amp; operator -- (int) { m_p--; return *this; } virtual iterator &amp; operator ++ () { ++m_p; return *this; } virtual iterator &amp; operator ++ () { m_p++; return *this; } virtual iterator &amp; operator += (int n) { m_p += n; return *this; } virtual iterator &amp; operator -= (int n) { m_p -= n; return *this; } virtual bool operator == (const iterator &amp; o) { return ((ptr_iterator*)&amp;o)-&gt;m_p == m_p; } virtual bool operator != (const iterator &amp; o) { return ((ptr_iterator*)&amp;o)-&gt;m_p != m_p; } private : byte * m_p; }; </code></pre> <p>Apart the forementioned iterator we can have an iterator that will iterate over an std::vector</p> <p>template class std_iterator : public iterator { typedef typename C::iterator c_iterator; public : iterator &amp; operator = (const c_iterator &amp; i) { m_it = i; return *this; } iterator &amp; operator = (const std_iterator &amp; o) { m_it = o.m_it; return *this; }</p> <pre><code>virtual iterator &amp; operator ++ () { ++m_it; return *this; } virtual iterator &amp; operator ++ (int) { m_it++; return *this; } virtual iterator &amp; operator -- () { --m_it; return *this; } virtual iterator &amp; operator -- (int) { m_it--; return *this; } virtual iterator &amp; operator += (int n) { m_it += n; return *this; } virtual iterator &amp; operator -= (int n) { m_it -= n; return *this; } virtual bool operator == (const iterator &amp;o) { return ((std_iterator*)&amp;o)-&gt;m_it == m_it; } virtual bool operator != (const iterator &amp;o) { return ((std_iterator*)&amp;o)-&gt;m_it != m_it; } bool operator == (const c_iterator &amp;i) { return m_it == i; } bool operator != (const c_iterator &amp;i) { return m_it != i; } </code></pre> <p>private :</p> <pre><code>c_iterator m_it; </code></pre> <p>};</p> <p>Ok, so basically the forementioned iterators are capable of iterating over an array or a vector. </p> <p>if we were to use a function that has the following interface :</p> <pre><code>int DoStuff(iterator &amp;it); </code></pre> <p>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 :)</p> <p>Thing is, I need the following functionality :</p> <p>std_iterator> it = my_vector.begin(); // THIS ONE WORKS std_iterator> other = it; // THIS ONE WORKS TOO</p> <p>If I were inside the forementioned function</p> <pre><code>int DoStuff(iterator &amp;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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload