int const * * p1= 0;
int * * p2 = 0;
p1 = p2;
p1 has more const restriction than p2 so increasing const'ness should
always be allowed, there's no harm which p1 could do to the value
pointed to by p2. So why is this an error.
Regards
You aren't really increasing or decreasing "const'ness". p1 is a
pointer to type A, and p2 is a pointer to type B. (Where A is a
pointer to a const int, and B is a pointer to int). In a way,
neither p1 or p2 have any const qualifiers at all (both are non
const).
Interestingly, if you change you first line to:
int const * const * p1 = 0;
and it compiles fine.
p1 has LESS restriction in one way: *p1 = &const_int is legal, while it's
not for p2.
int* p;
int ** p2 = &p;
int const** p1 = p2; // error...
int const c = 0;
*p1 = &c;
**p2 = 1; // ...because it would allow this to modify c
See section 4.4 Qualification Conversions, paragraph 4. In summary, you
can add const (and volatile) at any "depth", as long as you also add const
to all shallower levels. As stated in the standard, this is to ensure that
const safety isn't implicitly violated. The reasoning is that when you add
it at some level, it's not there in the original type therefore if the
pointer were changed to point to a const object, the object would be
non-const in the original type and thus allow modification. Therefore,
const must be added to all shallower levels to prevent the callee from
changing anything.
> Why does the following gives a compilation error
> int const * * p1= 0;
> int * * p2 = 0;
> p1 = p2;
Because it's illegal. It breaks const-ness without a cast.
> p1 has more const restriction than p2 so increasing const'ness
> should always be allowed, there's no harm which p1 could do to
> the value pointed to by p2. So why is this an error.
int const i = 42 ;
int * pi ;
int ** p1 = &pi ;
int const** p2 = p1 ;
*p2 = &i ;
*pi = 0 ;
What does pi point to? The converions of &pi to an int const**
allows making it point to a const object, without a const_cast.
In other words, it breaks const.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Why can't you assign a "pointer to int" to a "pointer to const int"?
It doesn't sound like this assignment would lose any qualifiers.
> >> Why does the following gives a compilation error
> >> int const * * p1= 0;
> >> int * * p2 = 0;
> >> p1 = p2;
> >> p1 has more const restriction than p2 so increasing
> >> const'ness should always be allowed, there's no harm which
> >> p1 could do to the value pointed to by p2. So why is this
> >> an error.
> > You aren't really increasing or decreasing "const'ness".
> > p1 is a pointer to type A, and p2 is a pointer to type B.
> > (Where A is a pointer to a const int, and B is a pointer to
> > int).
> Why can't you assign a "pointer to int" to a "pointer to const
> int"? It doesn't sound like this assignment would lose any
> qualifiers.
You can, but that's not what he's attempting to do. What he's
trying to do breaks const-ness; see my other posting.