C ++中volatile成员函数的目的是什么? [英] What is the purpose of a volatile member function in C++?

查看:158
本文介绍了C ++中volatile成员函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆