mutable
This keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.
class A
{
public:
void test()const
{
i = 3;
}
mutable int i;
};
mutable int i;//改为int i; error,左值指定 const 对象
mutable int i;//改为mutable const int i; error,“A::i”: 非法的存储类,左值指定 const 对象
mutable int i;//改为mutable static int i; error,“A::i”: 非法的存储类
volatile
说明变量在程序执行中可被隐含地改变,表明某个变量的值可能在外部被改变,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.