Search code examples
c++iteratorvolatile

how to declare volatile iterator in c++


Is there a way to declare an iterator which is a member variable in a class and that can be incremented using a member function even though the object of that class is const.


Solution

  • That would be with the "mutable" keyword.

    class X
    {
    public:
       bool GetFlag() const
       {
          m_accessCount++;
          return m_flag;
       }
    private:
       bool m_flag;
       mutable int m_accessCount;
    };