Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 507
user293666 Avatar asked Mar 15 '10 02:03

user293666


People also ask

What is volatile member function in C++?

Constant and volatile member functions (C++ only) A nonconstant member function can only be called for a nonconstant object. Similarly, a member function declared with the volatile qualifier can be called for volatile and nonvolatile objects. A nonvolatile member function can only be called for a nonvolatile object.

What is the use of member function?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

What are const member functions MC?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.


2 Answers

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.

like image 133
Michael Burr Avatar answered Nov 15 '22 13:11

Michael Burr


EDIT:

This answer was posted when the question was about the volatile keyword. Question seems to have been changed by a third party.

ORIGINAL:

Volatile informs the compiler that it should not assume that the value it just put in the variable marked as volatile will be there next time it uses it... that it must check the current value before using it again.

One example is if the variable represents a memory location that might be changed by another process.

Here's an example (been ages since I did C++ so please forgive any minor syntax issues):

volatile int x;  int DoSomething() {     x = 1;      DoSomeOtherStuff();      return x+1; // Don't just return 2 because we stored a 1 in x.                   // Check to get its current value } 
like image 32
Eric J. Avatar answered Nov 15 '22 13:11

Eric J.