成员函数是易失性的是什么意思? [英] What does it mean when a member function is volatile?

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

问题描述

我通常看到 const 说明符,用于指示const成员函数。但是,当使用 volatile 关键字是什么意思?

I normally see the const specifier used to indicate a const member function. But what does it mean when the volatile keyword is used?

void f() volatile {}

这对我来说很好,但是我不不明白这是干什么的。我在搜索中找不到有关此的任何信息,因此不胜感激。

This compiles fine for me but I don't understand what this is for. I couldn't find any information about this in my search so any help is appreciated.

更新:为了清楚起见,我知道什么 volatile 用于。我只是不知道在这种情况下是什么意思。

Update: To make it clear, I know what volatile is for. I just don't know what it means in this context.

推荐答案

volatile 限定词类似于 const 限定词。它允许在 volatile 对象上调用成员函数:

The volatile qualifier on a member function is analogous to the const qualifier. It allows the member function to be called on volatile objects:

struct A {
    void f() volatile {}
    void g() {}
};

int main() {
    A volatile a;
    a.f(); // Allowed
    a.g(); // Doesn't compile
}

这篇关于成员函数是易失性的是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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