定义 volatile 类对象 [英] Defining volatile class object

查看:22
本文介绍了定义 volatile 类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

volatile 可以用于类对象吗?喜欢:

Can the volatile be used for class objects? Like:

volatile Myclass className;

问题是它不能编译,调用某个方法时,到处都会出现错误:错误 C2662:函数":无法将this"指针从volatile MyClass"转换为MyCLass &"

The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &'

这里的问题是什么以及如何解决?

What is the problem here and how to solve it?

class Queue {
            private:
                struct Data *data;
                int amount;
                int size;
            public:
                Queue ();
                ~Queue ();
                bool volatile push(struct Data element);
                bool volatile pop(struct Data *element);
                void volatile cleanUp();
            };
    .....
    volatile Queue dataIn;

        .....

    EnterCriticalSection(&CriticalSection);
    dataIn.push(element);
    LeaveCriticalSection(&CriticalSection);

推荐答案

是的,你可以,但是你只能调用声明为 volatile 的成员函数(就像 const 关键字).例如:

Yes, you can, but then you can only call member functions that are declared volatile (just like the const keyword). For example:

 struct foo {
    void a() volatile;
    void b();
 };

 volatile foo f;
 f.a(); // ok
 f.b(); // not ok

<小时>

根据您的代码进行

bool volatile push(struct Data element);

声明一个 non-volatile 成员函数,它返回一个 bool volatile (= volatile bool).你想要

declares a non-volatile member function that returns a bool volatile (= volatile bool). You want

bool push(struct Data element) volatile;

这篇关于定义 volatile 类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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