C++ 中易失性成员函数的用途是什么?
C++ 中 易失性
成员函数的用途是什么?
What is the purpose of a volatile
member function in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C++ 中 易失性
成员函数的用途是什么?
What is the purpose of a volatile
member function in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
要回答有关“易失性成员函数”意味着什么的问题(可能是也可能不是发布问题的人最初的意图),请将成员函数标记为
const
或易失性
(或组合的const 易失性
)将这些限定符应用于函数中使用的this
指针。正如标准(9.2.1“this
指针”)所述:因此,通过将成员函数标记为
易失性
,您可以将该成员函数内的对象的非静态数据成员的任何访问设置为易失性
。To answer the question about what it means to have a 'volatile member function' (which may or may not be what was originally intended by the person who posted the question), marking a member function as
const
orvolatile
(or a combinedconst volatile
) applies those qualifiers to thethis
pointer used in the function. As stated by the standard (9.2.1 "Thethis
pointer"):So by marking the member function as
volatile
you'd be making any access to the non-static data members of the object within that member function asvolatile
.编辑:
这个答案是在问题是关于 volatile 关键字时发布的。问题似乎已被第三方更改。
原文:
Volatile 通知编译器,它不应该假设刚刚放入标记为 volatile 的变量中的值在下次使用它时会存在......它必须在再次使用它之前检查当前值。
一个例子是变量是否表示可能被另一进程更改的内存位置。
这是一个示例(自从我使用 C++ 以来已经很多年了,所以请原谅任何小的语法问题):
EDIT:
This answer was posted when the question was about the volatile keyword. Question seems to have been changed by a third party.
ORIGINAL:
Volatile informs the compiler that it should not assume that the value it just put in the variable marked as volatile will be there next time it uses it... that it must check the current value before using it again.
One example is if the variable represents a memory location that might be changed by another process.
Here's an example (been ages since I did C++ so please forgive any minor syntax issues):