Mutable Class Members, storage-class specifier mutable. - C++ Class

C++ examples for Class:Member Field

Description

Mutable Class Members, storage-class specifier mutable.

Demo Code

#include <iostream> 
using namespace std; 

class TestMutable 
{ 
public: /*from w ww .  j av  a2 s.c  om*/
   TestMutable( int v = 0 ) 
   { 
       value = v; 
   }

   int getValue() const 
   { 
       return ++value; // increments value 
   }
private: 
   mutable int value; // mutable member 
};

int main() 
{ 
   const TestMutable test( 99 ); 

   cout << "Initial value: " << test.getValue(); 
   cout << "\nModified value: " << test.getValue() << endl; 
}

Result


Related Tutorials