Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when should a member function be both const and volatile together?

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;
}
like image 679
aJ. Avatar asked Mar 17 '09 16:03

aJ.


People also ask

Can we use both const and volatile together?

Yes a C++ variable be both const and volatile. It is used in situations like a read-only hardware register, or an output of another thread. Volatile means it may be changed by something external to the current thread and Const means that you do not write to it (in that program that is using the const declaration).

Where we can use const and volatile together?

Another use for a combination of const and volatile is where you have two or more processors communicating via a shared memory area and you're coding the side of this communications that will only be reading from a shared memory buffer.

Can static member function be declared const volatile or const volatile?

A static member function cannot be declared with the keywords virtual , const , volatile , or const volatile . A static member function can access only the names of static members, enumerators, and nested types of the class in which it is declared.

Can the unsigned volatile change the value of constant variables?

An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) - at least through that particular name/pointer. The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object.


1 Answers

The cv qualification distilled means:

I won't change the value, but there is something out there that can.

You are making a promise to yourself that you won't change the value (const qualification) and requesting the compiler to keep its slimy hands off of this object and turn off all optimization (volatile qualification). Unfortunately, there is little standard among the compiler vendors when it comes to treating volatile fairly. And volatile is a hint to the compiler after all.

A practical use case of this is a system clock. Supposing 0xDEADBEEF was your system specific address of a hardware clock register you'd write:

int const volatile *c = reinterpret_cast<int *>(0xDEADBEEF);

You can't modify that register value, but each time you read it, it is likely to have a different value.

Also, can use this to model UARTs.

like image 62
dirkgently Avatar answered Oct 04 '22 04:10

dirkgently