Standard C++ refers to the type qualifiers const and volatile as cv-qualifiers. In both languages, the cv-qualifiers are only meaningful in expressions that are lvalues.
int * volatile x; /* x is a volatile pointer to an int */ int * const y = &z; /* y is a const pointer to the int variable z */
volatile int *x; /* x is a pointer to a volatile int */or
int volatile *x; /* x is a pointer to a volatile int */
const int *y; /* y is a pointer to a const int */or
int const *y; /* y is a pointer to a const int */
Declaration | Description |
---|---|
const int * ptr1; | Defines a pointer to a constant integer: the value pointed to cannot be changed. |
int * const ptr2; | Defines a constant pointer to an integer: the integer can be changed, but ptr2 cannot point to anything else. |
const int * const ptr3; | Defines a constant pointer to a constant integer: neither the value pointed to nor the pointer itself can be changed. |
You can put more than one qualifier on a declaration: the compiler ignores duplicate type qualifiers.
volatile struct omega { int limit; char code; }However, if a variable or variables are declared within the same definition of the type, a type qualifier can be applied to the variable or variables by placing it at the beginning of the statement or before the variable declarator or declarators. Therefore:
volatile struct omega { int limit; char code; } group;provides the same storage as:
struct omega { int limit; char code; } volatile group;In both examples, the volatile qualifier only applies to the structure variable group.
When type qualifiers are applied to a structure, class, or union, or class variable, they also apply to the members of the structure, class or union.