mutable explicit volatile

mutable 关键字

可以用来指出,即使结构或者类变量为const,其某个成员也可以被修改
 例如
struct data
{


char name[30];
mutable int accesses; 

.... };
const data veep = {"david";,0,}
strcpy(veep.name,"Jimmy");// not allowed
 veep.accesses++; // allowed

veep 的const限定符禁止程序修改veep的成员,但access成员的mutable说明符是的access不受这种限制

 

explicit关键字

C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生。声明为explicit的构造函数不能在隐式转换中使用。

// Copy From MSDN

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.

The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g.

// spec1_explicit.cpp // compile with: /EHsc #include class C { public: int i; explicit C(const C&) // an explicit copy constructor { printf("/nin the copy constructor"); } explicit C(int i ) // an explicit constructor { printf("/nin the constructor"); } C() { i = 0; } }; class C2 { public: int i; explicit C2(int i ) // an explicit constructor { } }; C f(C c) { // C2558 c.i = 2; return c; // first call to copy constructor } void f2(C2) { } void g(int i) { f2(i); // C2558 // try the following line instead // f2(C2(i)); } int main() { C c, d; d = f(c); // c is copied }
Note    explicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

 

// Copy From Internet Article

volatile 关键字

是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改。

用volatile关键字声明的变量i每一次被访问时,执行部件都会从i相应的内存单元中取出i的值。

没有用volatile关键字声明的变量i在被访问的时候可能直接从cpu的寄存器中取值(因为之前i被访问过,也就是说之前就从内存中取出i的值保存到某个寄存器中),之所以直接从寄存器中取值,而不去内存中取值,是因为编译器优化代码的结果(访问cpu寄存器比访问ram快的多)。

以上两种情况的区别在于被编译成汇编代码之后,两者是不一样的。之所以这样做是因为变量i可能会经常变化,保证对特殊地址的稳定访问。

=====以下为转载======

volatile关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编译器未知的因素更改

,比如:操作系统、硬件或者其它线程等。遇到这个关键字声明的变量,编译器对访问该变量的

代码就不再进行优化,从而可以提供对特殊地址的稳定访问。

使用该关键字的例子如下:
int volatile nVint;
  当要求使用volatile 声明的变量的值的时候,系统总是重新从它所在的内存读取数据,即

使它前面的指令刚刚从该处读取过数据。而且读取的数据立刻被保存。

例如:
volatile int i=10;
int a = i;
...
//其他代码,并未明确告诉编译器,对i进行过操作

int b = i;
  volatile 指出 i是随时可能发生变化的,每次使用它的时候必须从i的地址中读取,因而编

译器生成的汇编代码会重新从i的地址读取数据放在b中。而优化做法是,由于编译器发现两次从

i读数据的代码之间的代码没有对i进行过操作,它会自动把上次读的数据放在b中。而不是重新

从i里面读。这样以来,如果i是一个寄存器变量或者表示一个端口数据就容易出错,所以说vola

tile可以保证对特殊地址的稳定访问。
  注意,在vc6中,一般调试模式没有进行代码优化,所以这个关键字的作用看不出来。下面

通过插入汇编代码,测试有无volatile关键字,对程序最终代码的影响:
  首先,用classwizard建一个win32 console工程,插入一个voltest.cpp文件,输入下面的

代码:
 
#i nclude
void main()
{
 int i=10;
 int a = i;
 
 printf("i= %d/n",a);
 //下面汇编语句的作用就是改变内存中i的值,但是又不让编译器知道
 __asm {
  mov dword ptr [ebp-4], 20h
 }
 
 int b = i;
 printf("i= %d/n",b);
}     
然后,在调试版本模式运行程序,输出结果如下:
i = 10
i = 32
然后,在release版本模式运行程序,输出结果如下:
i = 10
i = 10
输出的结果明显表明,release模式下,编译器对代码进行了优化,第二次没有输出正确的i值。

下面,我们把 i的声明加上volatile关键字,看看有什么变化:
#i nclude
void main()
{
 volatile int i=10;
 int a = i;
 
 printf("i= %d/n",a);
 __asm {
  mov dword ptr [ebp-4], 20h
 }
 
 int b = i;
 printf("i= %d/n",b);
}     
分别在调试版本和release版本运行程序,输出都是:
i = 10
i = 32
这说明这个关键字发挥了它的作用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值