It sounds like you are looking for the keyword mutable
. A mutable member variable is modifyable within a const
member method. You can read about it here. An example:
class A
{
std::vector< int > v_;
mutable std::vector< int >::const_iterator current_;
public:
int get() const // note the const
{
assert( !v_.empty() );
if( current_ == v_.end() ) current_ = v_.begin();
return *current_++;
}
};
Note you still need const_iterator
as the vector itself is const and hence v_.begin()
does not give you a normal iterator. You could fix this by marking the vector itself also as mutable
, then you could use iterator
.
Here's a complete live example.