成员函数无法访问易失性成员

volatile member can not be accessed by member function

本文关键字:成员 易失性 访问 函数      更新时间:2023-10-16

以下代码使我的程序崩溃:

#include <string>
#include <vector>
class test {
volatile std::vector<std::string> wtf;
public:
    test() {}
    void dope() { wtf.clear(); }
};
int main(){
    (new test())->dope();
    return 0;
}

我不知道为什么。当我删除易失性时,它会再次工作。那么为什么易失性是一个问题呢?

std::vector::clear()没有

volatile限定符。

因此,用挥发性载体称呼它是非法的。

顺便说一句,volatile不是多线程的神奇关键字。
您可以使用mutex来保护对载体的访问。