Sunday, 24 April 2016

Mutable storage class specifier IN C++

 

Mutable:


                Usually data members cannot be modified when function is declared as const. If there is situation that even if function is declared data member need to be modified due to unavoidable situation. To achieve this declare the data member with mutable specifier.

#include<iostream>



class test
{
   public:
   int a;
   mutable int b;

   void updateConstFunc()const

   {
      // a=1; ==> Not possible since function is const
      b=2; //==> possible since mutable specified
   }
};

int main()
{
   test t;
   t.updateConstFunc();
   return 0;
}