I am writing a container class and have created a nested iterator class. Now I am putting in the const_iterator class. Other than a few typedefs, the implementation is near-identical. Do I really have to have the same code twice, or is there a way to have one class use the other's implementation?
Currently I am giving the iterator class a private constructor like so:
explicit iterator(const_iterator& it): internal{const_cast<pointer>(it.internal)}, begin{const_cast<pointer>(it.begin)}, end{const_cast<pointer>(it.end)}, cycle{it.cycle} {};
So in the const_iterator class, rather than rewrite the code, I have the following
const_iterator& operator+=(difference_type n) {return *this = iterator{*this} += n;}
I understand that this is technically not safe, but since users wouldn't be able to convert const_iterator to iterator is it ok?
A side question: If I go this route and have iterator constructible from const_iterator, should it be an operator or constructor?