What is the purpose of a volatile member function in C++?

user293666 picture user293666 · Mar 15, 2010 · Viewed 8.1k times · Source

What is the purpose of a volatile member function in C++?

Answer

Michael Burr picture Michael Burr · Mar 15, 2010

To answer the question about what it means to have a 'volatile member function' (which may or may not be what was originally intended by the person who posted the question), marking a member function as const or volatile (or a combined const volatile) applies those qualifiers to the this pointer used in the function. As stated by the standard (9.2.1 "The this pointer"):

The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

So by marking the member function as volatile you'd be making any access to the non-static data members of the object within that member function as volatile.