Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const conersions

0 views
Skip to first unread message

Rahul

unread,
Sep 28, 2008, 7:21:55 AM9/28/08
to
Hi,
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.

Regards

joseph cook

unread,
Sep 28, 2008, 7:58:58 AM9/28/08
to

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.

blargg

unread,
Sep 28, 2008, 3:33:31 PM9/28/08
to
In article
<4d3ff307-177b-466d...@q26g2000prq.googlegroups.com>, Rahul
<rahul...@lucent.com> wrote:

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.

James Kanze

unread,
Sep 29, 2008, 4:12:41 AM9/29/08
to
On Sep 28, 1:21 pm, Rahul <rahulsha...@lucent.com> wrote:

> 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

Juha Nieminen

unread,
Sep 28, 2008, 2:36:21 PM9/28/08
to
joseph cook wrote:
> On Sep 28, 7:21 am, Rahul <rahulsha...@lucent.com> wrote:
>> Hi,
>> 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.
>>
>> 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).

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.

James Kanze

unread,
Sep 29, 2008, 9:42:19 AM9/29/08
to
On Sep 28, 8:36 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> joseph cook wrote:
> > On Sep 28, 7:21 am, Rahul <rahulsha...@lucent.com> wrote:

> >> 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.

0 new messages