문제 설명
휘발성 멤버는 멤버 함수에서 액세스할 수 없습니다. (volatile member can not be accessed by member function)
The following code crashes my program:
#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;
}
And I have no idea why. When I remove volatile, it works again. So why is volatile a problem ?
참조 솔루션
방법 1:
std::vector::clear()
doesn't have volatile
qualifier.
So calling it with a volatile vector is illegal.
BTW, volatile
is not a magic keyword for multi‑threading. You may use mutex
to protect access to your vector.
(by user2741831、Jarod42)