"John Harrison" <jo*************@hotmail.com> wrote in message
news:ay****************@newsfe3-win.ntli.net...
I there a reliable and generic method to convert a const_iterator to an
iterator (i.e. something like const_cast).
I ask because I'm writing some methods which take and return iterators. A
const version of the method takes and returns const iterators and the
non-const version takes and returns non-const iterators. I'd like to avoid
code duplication like this
class X
{
iterator some_method(iterator i)
{
...
}
const_iterator some_method(const_iterator i) const
{
return const_cast<X*>(this)->some_method(i);
}
}
But that only works if I can convert a const_iterator to the equivalent
iterator.
Answers or alternatives welcome.
john
If iterator is derived from const_iterator, which in many cases is perfectly
practical, then you can do this:
class X
{
private:
iterator some_method_impl(const_iterator i) {...} // cast as required
here
public:
iterator some_method(iterator i) {return some_method_impl(i);} // bit
slice argument
const_iterator some_method(const_iterator i) const {return
some_method_impl(i);} // bit slice result
};
Of course I don't know if this would work with your particular iterators.
With many iterators in my experience, the bit sliced version is actually the
same data.
--
Cy
http://home.rochester.rr.com/cyhome/