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 161527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:13:44+00:00 2026-05-11T11:13:44+00:00

I was reading about volatile member function and came across an affirmation that member

  • 0

I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together. I didn’t get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together.

I wrote small class to test the same:

class Temp { public:      Temp(int x) : X(x)     {     }      int getX() const volatile     {         return X;     }      int getBiggerX()     {         return X + 10;     } private:     int X; };  void test( const volatile Temp& aTemp) {     int x = aTemp.getX(); }  int main(int argc, char* argv[]) {     const volatile Temp aTemp(10);     test(aTemp);      return 0; } 
  • 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. 2026-05-11T11:13:45+00:00Added an answer on May 11, 2026 at 11:13 am

    You asked for a practical example of volatile member functions. Well i can’t think of one because the only situations i could imagine are so low-level that i would not consider using a member function in the first place, but just a plain struct with data-members accessed by a volatile reference.

    However, let’s put a const volatile function into it just for the sake of answering the question. Assume you have a port with address 0x378h that contains 2 integers, 4 bytes each. Then you could write

    struct ints {     int first;     int second;     int getfirst() const volatile {         return first;     }      int getsecond() const volatile {         return second;     }       // note that you could also overload on volatile-ness, just like       // with const-ness };  // could also be mapped by the linker.  ints const volatile &p = *reinterpret_cast<ints*>(0x378L); 

    You are stating

    I’m not changing them, but another thing outside this abstract semantics could change it. So always do a real load from its address.

    Actually, volatile signals that the value of an object might not be the value last stored into it but is actually unknown and might have been changed in between by external (not observable by the compiler) conditions. So when you read from a volatile object, the compiler has to emulate the exact abstract semantics, and perform no optimizations:

    a = 4; a *= 2;    // can't be optimized to a = 8; if a is volatile because the abstract   // semantics described by the language contain two assignments and one load. 

    The following already determines what volatile does. Everything can be found in 1.9 of the Standard. The parameters it talks about are implementation defined things, like the sizeof of some type.

    The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. […]

    A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. […]

    The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

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

Sidebar

Ask A Question

Stats

  • Questions 257k
  • Answers 257k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Couple of points I don't understand that will be affecting… May 13, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer Yes, it is true, but it's a headache. When working… May 13, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer Boost's array is fixed-size based on the second template parameter,… May 13, 2026 at 10:41 am

Related Questions

I have code listed here: Threading and Sockets . The answer to that question
I'm reading Joe Duffy's post about Volatile reads and writes, and timeliness , and
I was reading about output buffering in JavaScript here , and was trying to
I was reading about App Engine on wikipedia and came across some GQL restrictions:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.