0

I was studying about const qualifier with the book C++ Primer by Stanley B. Lippman but when I read about top-level and low level consts I got confused in the part where it envolves pointers.

For example:

In this part of the code

const int ci = 42; //ok
const int *p2 = &ci; //ok
const int *const p3 = p2; //ok
int *p = p3; //error
p2 = p3; //ok

I understand that p3 has both, top-level and low-level const, because the object itself is const and the pointer can point to a const object.

I don't understand correctly why top-level consts are ignored and why I can assign p3 to p2 but I cannot use p3 to initialize p. Can anyone explain me, with more details, the reason why it happens?

PS: I already saw What are top-level const qualifiers? question and it didn't help me that much.

Community
  • 1
  • 1
Gabriel
  • 763
  • 1
  • 10
  • 28
  • Think of it this way: can you write to `*p2`? No, its const-ness prevents that, so the unwriteable `*p3` remains safe. But you *can* assign something to `*p`, which would write to `*p3`. Not allowed. – Paul Roub Aug 25 '15 at 18:32
  • 3
    `p2 = p3;` is harmless because you can't use `p2` to modify either `p3` or `ci`; const-correctness is preserved. On the other hand, if `int *p = p3;` were allowed, then you could do `*p = 84;` which would modify `ci`, thus violating const-correctness. – Igor Tandetnik Aug 25 '15 at 18:33
  • [One of my answers to another SO post](http://stackoverflow.com/a/28355561/434551) might be helpful. – R Sahu Aug 25 '15 at 18:37
  • @RSahu thank you for the sugestion, I will take a look! – Gabriel Aug 25 '15 at 18:40
  • @IgorTandetnik so we should always think about the future consequences, because even if I didn't change the value right now it can cause me future problems, right? – Gabriel Aug 25 '15 at 18:42
  • I think the names top-level and bottom-level are slightly confusing. The advice I can give you is look at the types to which the pointers point. If they match, you can assign them. If they don't, you can't. `p2` and `p3` are both pointers to `const int`, so you can assign `p3` to `p2`. `p` is a pointer to `int`, so you cannot assign it to either `p2` or `p3`. Note that you also cannot assign `p2` to `p3` (beyond initialization) because `p3` is a constant pointer. More precisely, it's a constant variable of type `pointer to const int`. – Haitham Gad Aug 25 '15 at 19:12
  • Not so much about future consequences, but about potential consequences - as in, "once I get this variable, what could I do with it?" The type system is designed in such a way as to prevent even a possibility of modifying a const value (unless you subvert it, e.g. with a cast, of course). – Igor Tandetnik Aug 25 '15 at 19:13
  • @HaithamGad It's not as simple as "types must match". You can go in the other direction - assign a pointer-to-non-const to pointer-to-const. The exact rules are spelled out in **[conv.qual]** and are pretty elaborate. – Igor Tandetnik Aug 25 '15 at 19:17
  • @IgorTandetnik ok, sure. I think I got it, thank you for the explanation and also to complement the answer made by Haitham, that could cause a very confusing situation. – Gabriel Aug 25 '15 at 19:37
  • @IgorTandetnik sadly I cannot even ask you to send your comment as an answer because for some reason my question was marked as a duplicate. I'm sorry for that but thank you for the help. – Gabriel Aug 25 '15 at 19:39

0 Answers0