สมาชิกข้อมูลที่เปลี่ยนแปลงได้คือสมาชิกที่มีค่าสามารถเปลี่ยนแปลงได้ในรันไทม์แม้ว่าอ็อบเจ็กต์จะเป็นประเภทคงที่ มันอยู่ตรงข้ามกับค่าคงที่
บางครั้งตรรกะจำเป็นต้องใช้สมาชิกข้อมูลเพียงหนึ่งหรือสองตัวเป็นตัวแปรและอีกตัวหนึ่งเป็นค่าคงที่ในการจัดการข้อมูล ในสถานการณ์นั้น ความไม่แน่นอนเป็นแนวคิดที่มีประโยชน์มากในการจัดการคลาส
ตัวอย่าง
#include <iostream>
using namespace std;
code
class Test {
public:
int a;
mutable int b;
Test(int x=0, int y=0) {
a=x;
b=y;
}
void seta(int x=0) {
a = x;
}
void setb(int y=0) {
b = y;
}
void disp() {
cout<<endl<<"a: "<<a<<" b: "<<b<<endl;
}
};
int main() {
const Test t(10,20);
cout<<t.a<<" "<<t.b<<"\n";
// t.a=30; //Error occurs because a can not be changed, because object is constant.
t.b=100; //b still can be changed, because b is mutable.
cout<<t.a<<" "<<t.b<<"\n";
return 0;
}