The code you've posted has fixed the type of iterators returned from Base
and all it's implementantions to std::vector<int>::iterator
which is probably not what you want. Jeremiah's suggestion is one way to go with one drawback: you loose compatibility with STL... I know of three implementations of a polymorphic iterator wrapper:
- becker's
any_iterator
(which implements boost::iterator_facade
)
- the
opaque_iterator
library (google for it), or
- Adobe's very interesting poly library which contains a hierarchy of STL conforming
any_iterator
s.
The problem is harder than it might seem... I made an attempt myself mainly because I needed covariance in any_iterators
type argument (any_iterator<Derived>
should be automatically convertible to any_iterator<Base>
) which is difficult to implement cleanly with STL like iterators. A C# like Enumerator<T>
is easier to implement(*) (and imho generally a cleaner concept than STL-like pairs of iterators) but again, you "loose" the STL.
(*) = without 'yield' of course :-)