mardi 25 août 2015

Why top-level consts are ignored when I copy an object? [duplicate]

This question already has an answer here:

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?

Aucun commentaire:

Enregistrer un commentaire