I'm having a brain cramp:
struct MyStruct
{
int x;
...
inline int getX1() const { return x; }
inline int getX2() const volatile { return x; }
};
volatile MyStruct myStruct;
I understand that the compiler will let me call myStruct.getX2() and won't let me call myStruct.getX1()
, because methods called on volatile structs/classes must have the volatile
qualifier on those methods.
Here's my question: If I create such a class, and I publish it for use by other software routines, what are the reasons I would add or not add a volatile qualifier on a method?
Is it because a method tagged volatile
tells the compiler not to assume any of its members are not volatile
, for optimization purposes, whereas if a method is not tagged volatile
, then any members not tagged volatile
can be optimized?