1

I'd like to present some idea on a simple code sample:

class Base{

public:
  void doSomeNastyThings(int param)
  {
    IteratorInterface *iter_face = iterators[param];
    for(auto it = iter_face->begin(); it != iter_face->end(); it++)
    {
      //do another nasty things
    }
  }

protected:
  std::map<int, IteratorInterface*> iterators;
};

And I wish to populate an iterators map from derived classes.... But how can I create such an IteratorInterface in STL style? All iterators will provide me an access to the same type (let me call it 'T'), but they can have different types:

std::map<T>::iterator, std::vector<T>::iterator, foo<T>::iterator.
Dejwi
  • 4,393
  • 12
  • 45
  • 74
  • There's no such thing as `std::map::iterator`, only many instances of `std::map::iterator`, for different element types. Consequently, the details very much depend on what exactly you want: element type being fixed in `IteratorInterface`, or `IteratorInterface` supporting any possible element type. – n. m. could be an AI Apr 11 '13 at 20:17
  • Ok, you are right about templated std::map::iterator. I will precise this part of question. – Dejwi Apr 11 '13 at 20:18
  • A class having `begin()` and `end()` is probably not an iterator, it is a container. – n. m. could be an AI Apr 11 '13 at 20:21
  • You are right one more time. But... no matter. I can declare std::map> to hold begin and end iterators. It is still the same problem. – Dejwi Apr 11 '13 at 20:26

2 Answers2

1

One approach would be to create template class derived from IteratorInterface that will do nasty things with iterator, implementing IteratorInterface and giving you implementation for each iterator you may want it to use with. But first you should read this post and probably rethink whole idea.

Community
  • 1
  • 1
alexrider
  • 4,449
  • 1
  • 17
  • 27
1

It is possible, but not recommended.

You will have to create an abstract base class template, say BaseIteratorImpl<T>, and for each different iterator type, create a concrete derived class, say VectorIteratorImpl<T> : public BaseIteratorImpl<T>. You also will want to create IteratorInterface<T> that holds and encapsulates a pointer to BaseIteratorImpl<T>, to provide the familiar value semantic for iterators.

More generically, instead of VectorIteratorImpl<T> and ListIteratorImpl<T> and ... one could create

class IteratorImpl<ContainerType> : 
    public BaseIteratorImpl<typename ContainerType::value_type> 

and use e.g. IteratorImpl<vector<double> >.

IteratorInterface<T> would forward all the usual iterator operations to its BaseIteratorImpl<T> member. BaseIteratorImpl<T> would have a pure virtual function for each of these operations. IteratorImpl<C> would implement these operations.

This is not recommended because you will have to write lots and lots of code, and only have a huge performance loss as the result.

Instead of iterators, you should do this operation (templatize+virtualize) with your algorithms (what happens inside doSomeNastyThings).

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243