简体   繁体   English

volatile函数与C ++中的常量成员函数

[英]volatile member function vs. constant member function in C++

A lot of people are saying "volatile member function is completely analogous to how const works." 很多人都说“易变成员函数完全类似于const的工作方式。”

They are quite similar in the sense of if a pointer is marked as const/volatile, it can only access member functions marked as const/volatile. 它们在指针被标记为const / volatile的意义上非常相似,它只能访问标记为const / volatile的成员函数。

But actually defining a member function as const has an additional effect, which makes the function read-only. 但实际上将成员函数定义为const会产生额外的影响,这会使函数成为只读函数。 Any modifications of the object inside the function will cause a compiler error. 对函数内部对象的任何修改都将导致编译器错误。 Is there such analogs in volatile member function? 在volatile成员函数中是否有这样的类比?

Well, a volatile member function will make the object members volatile, that is, this will be as if it were defined volatile T * const this . 好吧,一个volatile成员函数会使对象成员变为volatile,也就是说, this好像它被定义为volatile T * const this And as a consequence, any reference to a member variable is also volatile. 因此,对成员变量的任何引用也是不稳定的。

Remember that volatile read/writes are operations that cannot be elided/reordered by the compiler. 请记住,易失性读/写是编译器无法省略/重新排序的操作。 They are usually used to implement memory-mapped hardware devices or things like that. 它们通常用于实现内存映射的硬件设备或类似的东西。

Frankly speaking I've never been a use of this feature, other than doing smart tricks to filter the access to the function, not to make use of the volatile-ness of the object. 坦率地说,我从来没有使用过这个功能,除了做一些巧妙的技巧来过滤对函数的访问,而不是利用对象的易失性。 If your code is low level enough to need volatile you probably will want to go putting the volatile just in the variables you need. 如果你的代码低到足以需要volatile你可能会想要将volatile放在你需要的变量中。

Short answer: yes. 简短回答:是的。

If an instance of an object is declared volatile then it is an error to call non-volatile methods on it (or for those methods to call other non-volatile methods). 如果一个对象的实例被声明为volatile,那么在它上面调用非易失性方法是错误的(或者那些方法调用其他非易失性方法)。

A non-volatile instance can still call volatile methods, but not that it is perfectly legal to have two otherwise identical methods in a class - one volatile and one not. 非易失性实例仍然可以调用volatile方法,但并不是说在类中有两个相同的方法是完全合法的 - 一个是volatile,一个不是。 In that case a non-volatile instance will call the non-volatile version of the method. 在这种情况下,非易失性实例将调用该方法的非易失性版本。

But actually defining a member function as const has an additional effect, which makes the function read-only. 但实际上将成员函数定义为const会产生额外的影响,这会使函数成为只读函数。

That's a bit of a misconception. 这有点误解。 It doesn't make the member function read-only - it makes *this const . 它不会使成员函数成为只读 - 它使*this const There's a small, but important, difference between the two (the member function can still modify mutable members, and if it wants to be nasty, it can cast away the const -ness of *this to modify anything it wants, and the compiler won't complain). 两者之间有一个小但重要的区别(成员函数仍然可以修改mutable成员,如果它想要讨厌,它可以抛弃*thisconst -ness来修改它想要的任何东西,并且编译器赢了抱怨)。

And a volatile member function works in the exact same way - it makes *this volatile . 并且一个volatile成员函数以完全相同的方式工作 - 它使*this变得volatile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM