So, basically, I want a pure virtual method to return an iterator to an arbitrary set of a specific type, for example, in pseudocode:
virtual Iterator<T> getIterator() const = 0;
The user of this class does not really care what implementation the child class uses. It can be a set, vector, list, array, etc.
I know the std::iterator class, but I cannot find a way to specify it correctly to work with a simple vector.
virtual std::iterator<std::random_access_iterator_tag,T> getIterator() const = 0; myVector.begin()
defining std::iterator with const T , since the type parameter does not work either. I also tried to leave T and instead define the pointer and reference types as const T* and const T& .
Looking at the implementation of std::vector , I found that std::vector::const_iterator actually comes from _Iterator012 from _Iterator_base .
It really seems to me that there is no way to work with arbitrary collections in std. Implementing my classes as templates, such as <algorithm> , is not an option for me for two reasons:
- There is no control over the type of actual value
- I just donβt want my class templates to complicate my design significantly and make things less flexible.
The parameter T was just for demonstration, in fact it is a specific type.
source share