What is the difference between "const X a" and "X const a" if X is a class

I have a class name X, what is the difference between "const X a" and "X const a"

+3
source share
3 answers

Nothing.

A constqualifier refers to everything immediately to his left. If there is nothing to his left, then this applies to everything that is immediately to the right.

+13
source

In this case, there is no difference.

If you have a pointer or link, a change that may look almost the same is important. Given something like:

T * a;

The position const(or volatile) relative to the sprocket is significant:

T const * a;
T * const a;

, a const T (.. T, a). , a ( const const) T, .. , a, , . , :

T const * const a;

, T, .

+6

( ), .

: const '*', , , .

:

const int  a=1;  // 'a' value can't be changed
const int* q;    // the data that 'a' point to is constant
int const* q;    // the same
int* const p=&a; // the pointer is constant: const is behind '*'

int b=2;
p = &b; // error: trying to change constant pointer
+2

Source: https://habr.com/ru/post/1770671/


All Articles