mutable中文意思为“易变的”
mutable关键字作用:当类或者结构体中的某个变量被mutable关键字修饰时,即便这个结构体或者类被实例化为const类型,其中被mutable修饰的变量仍可以被修改。
例子:
typedef struct test {
mutable int iVal = 0;
}Test;
int main() {
std::cout << "test" << std::endl;
const Test t;
t.iVal = 2;
std::cout << t.iVal << std::endl;
getchar();
}
输出:2
如果iVal不是mutable修饰的变量,那么t.iVal=2这个语句会报错。