I have a custom iterator implementation (it iterates over a db query result, but that is irrelevant to this). It conceptually has 2 sets of functions: get type functions, which return a value from the current item (current row), and setup type functions (in my case binds), which set up the iterator before it starts the iteration, so a new iterator instance is actually not pointing to the first item yet. There is also a reset function that nullifies the iterator and readys for being setup differently.
When one of the get functions is called, it first checks if the iterator is still new or has been reset (effectively pointing to begin()-1) and if so, advances it to the first item. The get functions would (should) be const, except that that conditional first advancement prevents this.
Should I:
const and throw in a mutable for the isReset flag, orconst, oroperator++ once before calling any of the get functions?Your iterator has state (i.e. whether or not underlying data is available yet), and the "get" function alters that state. So that makes it just like any other iterator. The "get" function shouldn't be const. By the way, instead of calling your function "get", it would be better to make the iterator have the same interface as Standard Library iterators - then you can use standard algorithms on it. So use the operator*() and operator++() functions among others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With