Implementing an iterator for a set of handles
<p>I'm working with an awkward C library interface for a database containing a set of objects. The objects have a type and lets say that objects of type A contain a set of B objects and so on. Access to an object is via a handle which are defined as follows:</p>

<pre><code>typedef struct
{
    int handle;
} AHandleT; 


typedef struct
{
    int handle;
} BHandleT;
</code></pre>

<p>To iterate over an A object's B children, the following functions are used:</p>

<pre><code>ReturnT getB(AHandleT /*in*/, BHandleT* /*out*/)
ReturnT getBNext(BHandleT /*in*/, BHandleT* /*out*/)
</code></pre>

<p>Likewise for iterating over a set of B objects:</p>

<pre><code>ReturnT getC(BHandleT handle/*in*/, CHandleT* subHandle/*out*/)
ReturnT getCNext(CHandleT handle/*in*/, CHandleT* next/*out*/)
</code></pre>

<p>In order to work with this interface from C++ I have made the following iterator and I would appreciate your advice with regard to its implementation. Additionally, do you think this is a good approach? Keep in mind that I'm quite new to C++ and I will be writing my code using TDD.</p>

<pre><code>template&lt;class HandleT&gt;
class HandleIterator 
{
public:
    typedef ReturnT (*GetNext)(HandleT, HandleT*);

    HandleIterator(): m_isLast(true)
    {
    }

    template&lt;class ParentHandleT&gt;
    HandleIterator(const ParentHandleT parentHandle, ReturnT (*getFirstChild)(ParentHandleT, HandleT*), GetNext getNext): m_isLast(false), m_getNext(getNext)
    {
        ReturnT rc = getFirstChild(parentHandle, &amp;m_currentHandle);
        if(rc == NotExisting)
        {
            m_isLast = true;
        }
    }

    void operator++()
    {
        ReturnT rc = m_getNext(m_currentHandle, &amp;m_currentHandle);
        if(rc == NotExisting)
        {
            m_isLast = true;
        }
    }

    void operator++(int)
    {
        ++(*this);
    }

    const HandleT&amp; operator*() const
    {
        return m_currentHandle;
    }

    const HandleT* operator-&gt;() const
    {
        return &amp;m_currentHandle;
    }

    friend bool operator==(const HandleIterator&amp; left, const HandleIterator&amp; right)
    {
        return left.m_isLast == right.m_isLast;
    }

    friend bool operator!=(const HandleIterator&amp; left, const HandleIterator&amp; right)
    {
        return !(left == right);
    }

protected:
    HandleT m_currentHandle;
    bool m_isLast;
    GetNext m_getNext;
};
</code></pre>

<p>Once I have a handle, I can then get the data contained within the object using functions of the following form, from the C interface:</p>

<pre><code>ReturnT getAName(AHandleT)
ReturnT getBName(BHandleT)
ReturnT getBOnlyProprty(BHandleT)
</code></pre>

<p>But that's the next stage.</p>

<p>etc.</p>
