Description:
I cannot set a variable or change it while it is defined volatile (in Main). Removing volatile solves the problem, but I need my variable to be volatile.
My tries:
Really a lot of tries out there. Overloading operator=, making new operator= volatile, making new volatile method. Nothing actually worked so far.
My main:
int main() {
volatile PlaceParentConversion s(10.0); // remove volatile = no errors
std::cout << s.mX << std::endl;
s = PlaceParentConversion::IDENTITY_CONVERSION;
std::cout << s.mX << std::endl;
return 0;
}
My class:
class PlaceParentConversion {
public: //all public, easier to check
const static PlaceParentConversion IDENTITY_CONVERSION;
double mX;
PlaceParentConversion(const double x);
PlaceParentConversion(const PlaceParentConversion& other);
};
const PlaceParentConversion PlaceParentConversion::IDENTITY_CONVERSION(0);
PlaceParentConversion::PlaceParentConversion(const double x) : mX(x) {}
PlaceParentConversion::PlaceParentConversion(const PlaceParentConversion& other) : mX(other.mX) {}
Error:
‘volatile PlaceParentConversion’ as ‘this’ argument of ‘PlaceParentConversion& PlaceParentConversion::operator=(const PlaceParentConversion&)’ discards qualifiers [-fpermissive]
Define a
volatileassignment operator:(I’ve shortened your class name for readability.)
Here’s a more complete example:
The static cast in the final line makes GCC not issue a warning that no access is happening to the volatile object which is the result of the evaluation of the assignment expression: the standard says that in a void context there is no lvalue-to-rvalue conversion and thus no access. We make the conversion explicit.