C ++中volatile成员函数的目的是什么? [英] What is the purpose of a volatile member function in C++?
问题描述
C ++中 volatile
成员函数的目的是什么?
What is the purpose of a volatile
member function in C++?
推荐答案
要回答关于具有volatile成员函数(这可能是也可能不是发布该问题的人最初想要的)的含义的问题,将成员函数标记为 const
或 volatile
(或组合的 const volatile
)将这些限定符应用于 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
or volatile
(or a combined const volatile
) applies those qualifiers to the this
pointer used in the function. As stated by the standard (9.2.1 "The this
pointer"):
类X
的成员函数中的类型为X *
。如果成员函数被声明为const
,那么类型为const X *
code> volatile ,的类型为
volatile X *
如果成员函数声明为const volatile
,则其类型为const volatile X *
。
The type of this in a member function of a
class X
isX*
. If the member function is declaredconst
, the type of this isconst X*
, if the member function is declaredvolatile
, the type ofthis
isvolatile X*
, and if the member function is declaredconst volatile
, the type of this isconst volatile X*
.
因此,通过将成员函数标记为 volatile
,您可以访问非该成员函数中的对象的静态数据成员为 volatile
。
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 as volatile
.
这篇关于C ++中volatile成员函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!