-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathMutable_data_members.cpp
50 lines (41 loc) · 1.48 KB
/
Mutable_data_members.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
/*
Mutable Data Members
Since, constant data members cannot be changed even in non const object,
a mutable data member, can be changed in a const object.
It has been introduced to particularly model constant concept. Example : Class which returns value of pi
Why mutable exists ?
Ans : If I declare an object to be const, then nothing can be changed in it. It means whatever bit-pattern that particular object
has got during inisialization, it will remain same. Sometimes it's difficult to work with it.
Note:
-mutable is applicable only on data members but not on any simple variable
-reference data members, static data members and const data members can not be declared as mutable.
-If a data member is declared mutable, then it's legal to assign a value to it from a const member function
*/
class MyClass {
int mem_;
mutable int mutableMem_;
public:
MyClass(int m , int mm) : mem_(m), mutableMem_(mm) {}
int getMem() const {
return mem_;
}
void setMem(int i) {
mem_ = i;
}
int getMutableMem() const {
return mutableMem_;
}
void setMutableMem(int i) const { //okay to change mutable
mutableMem_ = i;
}
};
int main() {
const MyClass myConstObj(1, 2);
cout << myConstObj.getMem() << endl;
myConstObj.setMem(3); //Error to invoke
cout << myConstObj.getMutableMem() << endl;
myConstObj.setMutableMem(4);
return 0;
}