Given the following class:
class MyClass {
public:
int value() const {
return value_;
}
private:
volatile int value_;
};
Does the value() member function also have to be marked as volatile to avoid getting optimized away or is it okay as written? Thanks.
Marking the member function volatile will have no effect on whether it is optimized away or not. It is fine as written.
It sounds like what you want is to learn about atomic variables. Take a look at std::atomic.
If you really want to learn about volatile, read this paper: http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf