I have an interrupt in my code which calls several member functions, and these member functions in some cases modify member variables of a class. The member variables are marked as volatile, because they are modified outside the normal flow of the program, but I would like to know if the functions themselves need to be marked as volatile? From a bit of reading, it would seem like it's not required because the object itself does not have to volatile, only some of its members, but I just want to check.
Asked
Active
Viewed 44 times
0
-
1I am 99% sure you don't want to mark your functions as volatile unless they are specifically returning volatile data. volatile is applied to the return type, not the function itself. – Michael Dorgan Sep 18 '20 at 19:48
-
Michael, I was less certain than that, but it's good to have as secon opinion. bnaecker, that question is a reverse of mine. In this situation, the members should be volatile, but perhaps not the function. – user4913118 Sep 18 '20 at 19:58
-
@MichaelDorgan Note that there is an extreme difference between a volatile member function and a function with a volatile return type. You can INDEED apply volatile to the member function itself! (See the linked duplicate) – Daniel Jour Sep 18 '20 at 20:06
-
@user4913118 Can you try to clarify why the question linked by bnaecker does NOT answer your question? – Daniel Jour Sep 18 '20 at 20:07
-
@daniel-jour It doesn't answer my question because if I mark the function volatile, how does the compiler know that the members are volatile in all other contexts outside the function? The answer states "...you may want to mark x and y volatile anyway if you really do want all accesses to them to be treated as volatile." I have already established it I want them volatile, but there is no mention in that question of whether or not the function should be volatile. – user4913118 Sep 18 '20 at 20:56