我看到它是可能的,但我不理解它的兴趣。
发布于 2010-06-13 06:48:33
const和volatile听起来像是在一个变量上引用了相同的概念,但事实并非如此。const变量不能被当前代码更改。volatile变量可能会被当前代码之外的某个外部实体更改。可以有一个const volatile变量-特别是像内存映射寄存器这样的变量-在程序无法预测的时间由计算机更改,但您的代码不允许直接更改。您可以使用const_cast向变量添加或删除const或volatile (“cv-volatile”)。
发布于 2010-06-13 06:49:39
const和volatile是正交的。
const表示数据是只读的。
volatile意味着变量可能会因为外部原因而改变,因此每次引用变量时,编译器都需要从内存中读取该变量。
因此,删除const允许您编写一个只读位置(代码必须具有一些特殊知识,该位置实际上是可修改的)。你不应该删除volatile来写它,因为你可能会导致未定义的行为(由于7.1.5.1/7 - If an attempt is made to refer to an object defined with a volatile-qualified type through the use of an lvalue with a non-volatile-qualified type, the program behaviour is undefined.)
https://stackoverflow.com/questions/3030382
复制相似问题