I must construct a graph from 2 different custom types **that I cannot change**.  

Because their signatures and purpose are almost the same I have some code duplication I would like to remove.  

Problem is they return a different iterator.

```
for (auto itFrom = m_pModelSpace->newIterator(); !itFrom->done() && nodesPassed < maxNodes; itFrom->step(), ++nodesPassed)
{
    /*
    ...
    */
    for (auto itTo = m_pModelSpace->newIterator(); itTo->objectId() != itFrom->objectId(); itTo->step())
    {
        /*
        ...
        */
    }
}

for (auto itFrom = m_pSelectionSet->newIterator(); !itFrom->done() && nodesPassed < maxNodes; itFrom->next(), ++nodesPassed)
{
    /*
    ...
    */
    for (auto itTo = m_pSelectionSet->newIterator(); itTo->objectId() != itFrom->objectId(); itTo->next())
    {
        /*
        ...
        */
    }
}

```

The comments are exactly the same code.  

Is there any way to work around this?  

I was thinking about a wrapper with 2 constructors for each type but then I also need a wrapper for the object iterator.  

Another idea was multiple inheritance of the 2 types but that also doesn't really feel right.

Any advice?

**Summurarized solution**  

Thanks to Mark B I have come to the following solution: 

```
namespace {
OdDbObjectIteratorPtr get_iterator(OdSmartPtr<OdDbBlockTableRecord>& pEntityCollection)
{
    return pEntityCollection->newIterator();
}

OdDbSelectionSetIteratorPtr get_iterator(OdSmartPtr<OdSelectionSet>& pEntityCollection)
{
    return pEntityCollection->newIterator();
}

void increment_iterator(OdDbObjectIteratorPtr& iter)
{
    iter->step();
}
void increment_iterator(OdDbSelectionSetIteratorPtr& iter)
{
    iter->next();
}

}

namespace spax{
template <typename Collection>
void ConnectionGraph::ConstructGraph(Collection& pEntityCollection, int maxNodes)
{
    // ...
    for (auto itFrom = get_iterator(pEntityCollection); !itFrom->done() && nodesPassed < maxNodes; increment_iterator(itFrom), ++nodesPassed)
    {
        //...
        for (auto itTo = get_iterator(pEntityCollection); itTo->objectId() != itFrom->objectId(); increment_iterator(itTo))
        {
            //...
        }
    }
}
}

```

Because the Selection set returned a parent OdSelectionSetIteratorPtr of OdDbSelectionSetIteratorPtr I added another function inspired by this solution to get the iterators.
Thanks for the help, I'm really pleased with the outcome.