I created a read only iterator which allows me to use it in a for loop more conveniently then with the std iterators, similar to what boost does with the FOREACH (not as good but good enough :))
Now it looks like this:
for(ReadOnylIterator<MyClass *> class = parent.getIterator(); class.end(); ++class)
class->function();
The problem that I have now is, that I must implement a function on the parent which returns the iterator. Since the std containers have all the same syntax, I was wondering if it is possible to define a copy constructor/assignment operator on the Iterator that accepts any of the std:: containers and creates the copy itself, instead of requiring the class to return it.
Of course I want to avoid having to define all of them myself like as there are lots of them:
ReadOnlyIterator<T> &operator=(std::list<T> const &v)
ReadOnlyIterator<T> &operator=(std::vector<T> const &v)
...
Is there a way to do this? When I look at the source of the vector I don't see a common base class, so I think it might not be posssible.
I don't understnad why the assignment operator doesn't work.
In my code I test it like this:
std::vector<SimpleClass *>t;
ReadOnlyIterator<SimpleClass *> &it = t;
And I get
error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'ReadOnlyIterator<T> &'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…