![](/img/trans.png)
[英]Calling volatile member function using not volatile object in C++
[英]volatile + object combination disallowed in C++?
我正在为TI TMS320F28335使用嵌入式编译器,因此我不确定这是否是一般C ++问题(没有运行C ++编译器)或仅仅是我的编译器。 将以下代码片段放在我的代码中会给我一个编译错误:
"build\main.cpp", line 61: error #317: the object has cv-qualifiers that are not
compatible with the member function
object type is: volatile Foo::Bar
当我注释掉下面的initWontWork()
函数时,错误消失了。 什么是告诉我的错误,如何解决它而不必使用在volatile struct
上运行的static
函数?
struct Foo
{
struct Bar
{
int x;
void reset() { x = 0; }
static void doReset(volatile Bar& bar) { bar.x = 0; }
} bar;
volatile Bar& getBar() { return bar; }
//void initWontWork() { getBar().reset(); }
void init() { Bar::doReset(getBar()); }
} foo;
以同样的方式你不能这样做:
struct foo
{
void bar();
};
const foo f;
f.bar(); // error, non-const function with const object
你不可以做这个:
struct baz
{
void qax();
};
volatile baz g;
g.qax(); // error, non-volatile function with volatile object
你必须对这些功能进行cv认证:
struct foo
{
void bar() const;
};
struct baz
{
void qax() volatile;
};
const foo f;
f.bar(); // okay
volatile baz g;
g.qax(); // okay
所以对你来说:
void reset() volatile { x = 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.