定义易失性类对象
易失性可以用于类对象吗? 就像:
volatile Myclass className;
问题是它无法编译, 当调用某个方法时,到处都会出现错误: 错误 C2662:“函数”:无法将“this”指针从“易失性 MyClass”转换为“MyCLass &”
这里有什么问题以及如何解决它?
编辑:
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);
Can the volatile be used for class objects?
Like:
volatile Myclass className;
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?
EDIT:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以,但是您只能调用声明为
volatile
的成员函数(就像const
关键字)。例如:根据您的代码进行编辑:
声明一个非-
易失性
成员函数,该函数返回bool 易失性
( =易失性布尔
)。你想要Yes, you can, but then you can only call member functions that are declared
volatile
(just like theconst
keyword). For example:Edit based on your code:
declares a non-
volatile
member function that returns abool volatile
(=volatile bool
). You want我认为他的意思是说
而不是“
也看看这里” http://www.devx.com/提示/提示/13671
I think he meant to say
instead of
Also have a look here http://www.devx.com/tips/Tip/13671
在C++语法中,“volatile”和“const”被称为“CV修饰符”。这意味着从语法的角度来看,“易失性”的工作方式与“const”完全相同。
您可以将所有“易失性”替换为“const”,然后您就可以理解为什么您的代码可以编译或无法编译。
In C++ grammar, "volatile" and "const" are called "CV modifiers". That means "volatile" works in exact the same way as "const" from syntactic point of view.
You can replace all "volatile" with "const" then you can understand why your code compiles or not.
是的。我们可以使用。请查看修改后的代码。
我希望它现在应该可以工作。
Yep. We can use. Please see the modified code.
I hope it should work now.