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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:00:31+00:00 2026-05-26T03:00:31+00:00

I asked this function based on this concept (maybe incorrect?!): Wherever a const can

  • 0

I asked this function based on this concept (maybe incorrect?!): Wherever a const can exist, a volatile can exist at the place.

class classA
{
public:
    const int Foo() const;
}

Here the first “const” means the return value is const, we can not change it.
the second const means “Is Query”, this function can not change the member variable and can not call non-const function.

Now comes to volatile: I can understand what volatile does on a variable, like “volatile int a;”
However I have no idea of the difference among the following:

Case 1: The return type is volatile?
volatile void Function1();

Case 2: The function can only call volatile functions? Why add volatile here? Any example?
void Function2() volatile;

Case 3:   Is it valid? If yes, is it simply a combination of Case 1 and Case 2?
volatile void Function3() volatile;

When we put the const at the end of function declaration, it has a beautiful name: “Is Query”
Can you give a decent name/alias to the “volatile” in Case 2?
I mean, whenever we call this name, we can know we are talking about Case 2, not case 1.

Thank you in advance!

  • 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-26T03:00:31+00:00Added an answer on May 26, 2026 at 3:00 am

    Volatile has one primary function, which is to say “STOP! This object is connected to external resources, and thus when I write to it, don’t reorder the write with respect to other volatile reads or writes, and when I read to it, don’t reorder it likewise and don’t optimize these away!”.

    To support this, you can put volatile after member functions, which is necessary to call member functions on volatile class objects.

    // just a silly example
    struct HWOverlayClock {
      HWOverlayClock() { }
    
      int64_t getTime() volatile const { return timestamp; }
    
      int64_t timestamp;
    };
    
    // imagine we use an implementation defined way to put the
    // object at some fixed machine address
    volatile const HWOverlayClock clock __attribute__((at_address(0xbabe)));
    

    Putting volatile on a return value can be done too, but it seems to me that it would be less useful since return values are usually temporaries and volatile semantics are quite on the the opposite scale of temporaries. Putting volatile on void is particularly nonsensical (const and volatile are ignored if put at the top-level of a return type if the type is not a class type (i.e at the right of a * if it is a pointer), because these return values do not correspond to memory, likely being kept in registers by implementations too). Putting volatile on non-toplevel for references or pointers can be useful, like in the following

    struct Controller {
        HWOverlayClock volatile const* getClock() const { return clock; }
    
    private:
        volatile const HWOverlayClock *clock;
    };
    

    Hope it helps.

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

Sidebar

Related Questions

I asked this Can I automate creating a .NET web application in IIS? a
I can see that this question has been asked several times, but none of
I realize this has been asked hundreds of times, however, I can't seem to
(I asked this question in another way , and got some interesting responses but
I asked this question before, Here however I think I presented the problem poorly,
I asked this question a while back but now I'm looking to implement an
I asked this question: Serial Port (rs232) in Mono for multiple platforms and this
I asked this question: Is ASP.NET MVC destined to replace Webforms? and a few
I asked this question earlier. I am intrigued by std::set but I have another
I asked this question to multiple people and until now I do not have

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.