[英]why can't a volatile object call nonvolatile member function
Why can't a volatile
object call a non- volatile
member function? 为什么volatile
对象不能调用非易失volatile
成员函数?
In case of const
, it makes sense that calling a non- const
member function violates the constness of the the object and hence it is prohibited. 在的情况下const
,是非常有意义的调用非const
成员函数违反对象的常量性 ,因此它被禁止。 But why in the case of volatile
? 但为什么在volatile
?
In case of const, it makes sense that calling a non-const member function violates the const-ness of the the object and hence it is prohibited. 在const的情况下,调用非const成员函数违反了对象的常量是有道理的,因此它是被禁止的。 But why in the case of volatile? 但为什么在波动?
It's just the same for volatile. 对于不稳定的情况也是如此。 Volatile means every access to an object is a visible side effect and cannot be eliminated. 易失性意味着对对象的每次访问都是可见的副作用,无法消除。 If you called a nonvolatile method on a volatile object, you would violate this property (because the nonvolatile method would treat the object just as a normal object). 如果在易失性对象上调用非易失性方法,则会违反此属性(因为非易失性方法会将对象视为普通对象)。 Therefore, it is impossible. 因此,这是不可能的。
From the standard: 从标准:
7.1.5.1. 7.1.5.1。 If an attempt is made to refer to an object defined with a volatile-quailified type through the use of an lvalue with a non-volatile-quailified type, the program behaviour is undefined. 如果尝试通过使用具有非volatile-quailified类型的左值来引用使用volatile-quailified类型定义的对象,则程序行为是未定义的。
I'm guessing your compiler posts an error to prevent undefined behavior. 我猜你的编译器发布了一个错误来防止未定义的行为。 The standard stating so should be reason enough. 标准陈述应该是足够的理由。
The volatile
qualifier works much in the same way as const
works. volatile
限定符的工作方式与const
工作方式相同。 To see what can be done thanks to this, take a look at this Alexandrescu article . 要了解这可以做些什么 ,请看看这篇Alexandrescu文章 。 That article should also give you some insight into the why . 那篇文章也应该让你对原因有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.