110

If I have:

struct whatever {
int data;
};
volatile whatever test;
will test.data be volatile too?

2
  • 4
    Excellent question. I read that having a volatile member function only makes the this pointer volatile, so members will be read from memory each time they are accessed but are not "officially" volatile. Dec 18, 2010 at 19:09
  • 2
    Possibly a duplicate, but still a good question. Dec 18, 2010 at 19:15

2 Answers 2

140

Another question can be asked (or simply another way to look at the original question):

Does making a struct const make all its members const?

If I have:

struct whatever { int data; };

const whatever test;

Will test.data be const too?

My answer is : Yes. If you declare an object of type whatever with const then all its members will be const too

Similarly, if you declare an object of type whatever with volatile then all its members will be volatile too, just like if you declare the object with const, all it's member will be const too.

const and volatile are two faces of the same coin; they're so that the Standard often refers to them as cv-qualifiers.


Quoting from the Standard ($7.1.5.1/8)

[Note: volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation. See 1.9 for detailed semantics. In general, the semantics of volatile are intended to be the same in C + + as they are in C. ]

That means, if your object is an instance of a struct, then the compiler cannot avoid aggressive optimization involving the object, unless it avoids aggressive optimization of each of it's members. (Otherwise, how else it can avoid optimization involving the object?)


Related topic:

Why do we use volatile keyword in C++?

3
  • 2
    It is important to note that the constness is propagated to immediate memembers only. If your struct holds a pointer to some object, the pointer itself will be const (e.g.. you won't be able to reassign it), but you will be able to change the object being pointed at. (assuming your struct isn't hoding a pointer to const) Dec 31, 2018 at 12:42
  • If you have a const object, all its members aren't const. For example, you can have mutable members.
    – user904963
    Mar 8, 2022 at 2:01
  • @PabloArias The const propagates because if class A has a member b of class B, this member will be const, so b's member's will be const as well. With a pointer, only the address pointed to is constant. By dereferencing it, you are not modifying the pointer itself (which is constant), but the object it points to. Aug 27, 2022 at 6:35
-3

From: http://msdn.microsoft.com/en-us/library/145yc477%28v=vs.80%29.aspx

To declare the object pointed to by the pointer as const or volatile, use a declaration of the form:

const char *cpch;
volatile char *vpch;

To declare the value of the pointer — that is, the actual address stored in the pointer — as const or volatile, use a declaration of the form:

char * const pchc;
char * volatile pchv;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.