Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:17:39+00:00 2026-05-20T10:17:39+00:00

If I have a C++ method declaration as follows: class A { public: double

  • 0

If I have a C++ method declaration as follows:

class A
{
public:
   double getPrice() volatile;
};
  1. What does volatile represent here?
  2. What could it be used for?

You might be interested in this Dr Dobbs article by Andrei Alexandrescu. I was 🙂

Edit:
That article was written a while back and now it seems that the community has moved on. Herb Sutter has this to say this. Thanks Iain (and Herb!)

@metal points out that Andrei had a follow up article here where he continues to advocate the use of volatile correctness as a valuable tool for detecting race conditions on systems supporting POSIX-like mutexes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:17:39+00:00Added an answer on May 20, 2026 at 10:17 am

    You’re probably familiar with const methods and const-correctness (cf. “Item 15 – Use const proactively” in C++ Coding Standards by Sutter and Alexandrescu), and volatile works in similar but slightly different ways to yield what might be called “volatile-correctness.”

    Like const, volatile is a type modifier. When attached to a member function as in your example, either modifier (or both!) mean that the object on which the method is called must have or be convertible to that type.

    Consider:

    struct A
    {
      void f();
      void cf() const;
      void vf() volatile;
      void cvf() const volatile;
      // ...
    };
    
    void foo( A& a, const A& ca, volatile A& va, const volatile A& cva )
    {
      a.f();    // Ok
      a.cf();   // Ok: Can convert non-const    obj to const    obj
      a.vf();   // Ok: Can convert non-volatile obj to volatile obj
      a.cvf();  // Ok: Can convert non-cv       obj to cv       obj
    
      ca.f();   // Error: can't call non-const method on const obj
      ca.cf();  // Ok
      ca.vf();  // Error: can't call non-const method on const obj
      ca.cvf(); // Ok: Can convert
    
      va.f();   // Error: can't call non-volatile method on volatile obj
      va.cf();  // Error: can't call non-volatile method on volatile obj
      va.vf();  // Ok
      va.cvf(); // Ok: Can convert
    
      cva.f();   // Error: can't call non-cv method on cv obj
      cva.cf();  // Error: can't call non-cv method on cv obj
      cva.vf();  // Error: can't call non-cv method on cv obj
      cva.cvf(); // Ok
    }
    

    Note these are compile-time errors, not run-time errors, and that is where it’s potential usefulness comes in.

    Const-correctness prevents unintentional errors at compile-time as well as making code “easier to understand, track, and reason about” (Sutter and Alexandrescu). Volatile-correctness can function similarly but is much less used (note that const_cast in C++ can cast away const, volatile, or const volatile, but rather than calling it cv_cast or similar, it’s named after const alone because it is far more commonly used for casting away just const).

    For instance, in “volatile – Multithreaded Programmer’s Best Friend”, Andrei Alexandrescu gives some examples of how this can be used to have the compiler automatically detect race conditions in multithreaded code. It has plenty of explanation about how type modifiers work, too, but see also his follow-up comments in his subsequent column.


    Update:

    Note that C++11 changes the meaning of const. Thus sayeth the Sutter: “const now really does mean ‘read-only, or safe to read concurrently’—either truly physically/bitwise const, or internally synchronized so that any actual writes are synchronized with any possible concurrent const accesses so the callers can’t tell the difference.”

    Elsewhere, he notes that while C++11 has added concurrency primitives, volatile is still not one of them: “C++ volatile variables (which have no analog in languages like C# and Java) are always beyond the scope of this and any other article about the memory model and synchronization. That’s because C++ volatile variables aren’t about threads or communication at all and don’t interact with those things. Rather, a C++ volatile variable should be viewed as portal into a different universe beyond the language — a memory location that by definition does not obey the language’s memory model because that memory location is accessed by hardware (e.g., written to by a daughter card), have more than one address, or is otherwise ‘strange’ and beyond the language. So C++ volatile variables are universally an exception to every guideline about synchronization because are always inherently “racy” and unsynchronizable using the normal tools (mutexes, atomics, etc.) and more generally exist outside all normal of the language and compiler including that they generally cannot be optimized by the compiler…. For more discussion, see my article ‘volatile vs. volatile.'”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a delegate declaration as follows... Public Class MyClass Public
Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
I need a Regex that will match a java method declaration. I have come
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate
I have a code snippet as public class ThreadStates { private static Thread t1
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
I have this delegate declaration: public delegate IEnumerable<T> SearchInputTextStrategy<T, U>(string param); Lets assume I
I have a view model public class DeviceModelEntryViewModel { public int ID { get;
I have a form (todo list) containing labels/checkboxes (todos) as follows: <form class=tasklist> These

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.