简体   繁体   English

为什么volatile对象不能调用非易失性成员函数

[英]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.

相关问题 C ++易失性对象,非易失性成员 - C++ volatile object, nonvolatile member 成员函数无法访问volatile成员 - volatile member can not be accessed by member function 为什么在某些情况下我可以在没有 object 的情况下调用成员 function? - Why in some cases can I call a member function without an object? 为什么派生类不能在此代码中调用受保护的成员函数? - Why can't a derived class call protected member function in this code? 在C ++中使用非volatile对象调用volatile成员函数 - Calling volatile member function using not volatile object in C++ 您可以从volatile函数中丢弃成员上的volatile吗? - Can you cast away volatile on a member from a volatile function> 为什么不允许常量子表达式消除const非易失性成员函数? - why not allow common subexpression elimination on const nonvolatile member functions? 为什么此公共成员函数不能在类内部声明的私有struct成员上调用decltype? - Why can't this public member function call decltype on a private struct member declared inside the class? 为什么我不能在这样的静态成员函数中调用静态成员变量? - why i can't call a static member variable in an static member function like this? 无法在非成员 function 中调用成员 function - Can't call Member function in a non member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM