以下代码使我的程序崩溃:
#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;
}
我不知道为什么。当我删除 volatile 时,它会再次工作。那么为什么 volatile 是个问题?
最佳答案
std::vector::clear()
没有 volatile
限定符。
所以用volatile vector调用它是非法的。
顺便说一句,volatile
不是多线程的神奇关键字。
您可以使用 mutex
来保护对您的 vector 的访问。
关于c++ - volatile 成员不能被成员函数访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850124/