Using the mutable keyword to remove the constant-ness of the function in C++
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info:
To do: Removing a function constant-ness to make it modifiable
To show: How to use the mutable keyword to remove the constant-ness of a function
// Using mutable to remove the const-ness of the function
#include <iostream>
using namespace std;
class Test
{
// using mutable
mutable int count;
mutable const int* ptr;
public:
// Read only function can't change const arguments.
int funct(int num = 10) const
{
// should be valid expression...
count = num+=3;
ptr = #
cout<<"After some operation, the new value: "<<*ptr<<endl;
return count;
}
};
int main(void)
{
Test var;
cout<<"Initial value of the argument is: 10"<<endl;
var.funct(10);
return 0;
}
Output example:
Initial value of the argument is: 10
After some operation, the new value: 13
Press any key to continue . . .