Reputation: 4592
If you have a class member function marked volatile, is it possible to cast away volatile on a class member when it's bein used within that function?
Upvotes: 0
Views: 173
Reputation: 208353
You can cast away volatile
from any context by using const_cast
. You are asking precisely about casting away inside a volatile
member, but that does not make any difference.
The volatile
in the function is a check that tells the compiler not to complain if you try to call that method on a volatile
object (or through a reference or pointer to volatile
object), which is unrelated to the volatile
-ness of the members.
What I am trying to say is that if you expect the behavior while accessing data members to be consistent with volatile
semantics just because the code is inside a volatile
member method, that won't happen.
Upvotes: 0
Reputation: 361472
Yes. To cast away the volatile-ness of an object, const_cast
is used:
T & t = const_cast<T&>(volatile_t);
This is the way. But whether you should use it in your code or not, I cannot say without looking at the code. In general, casting away the const-ness as well as volatile-ness, is a dangerous idea, and should be done only after very careful examination of all cases.
Upvotes: 4