Top-level const and Low-level const

Hello, I am working through the C++ Primer book and have come upon the code snippet that I have pasted below. Specifically this is dealing with const as it relates to pointers.

So you will know where I am starting from. The book states, "We use the term top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low level const.

1
2
3
4
5
6
int i = 0;
int *const p1 = &i; // we can't change the value of p1; const is top-level
const int ci = 42; // we cannot change ci; const is top-level
const int *p2 = &ci; // we can change p2; const is low-level
const int *const p3 = p2; // right most const is top-level, left-most is not
const int &r = ci; // const in reference type is always low-level 


I'm confused about the 4th line of code. It says that we can change p2, const is low-level. As I understand this, what it is saying is that the pointer (p2) can be changed to point to something else but the item that p2 points to can't be changed. Is this correct? Otherwise what is the const referring to at the beginning of the line? Thanks!
You are correct. const modifiers should be read right to left:
int     const          *        const
int   constant   pointer to   constant
<———————————————————————————————
constant pointer to constant int
(const int * and int const * are equivalent)
Last edited on
Thank you for your answer! I don't know if this forum gives you credit for correct answers.
Topic archived. No new replies allowed.