[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

const conersions

Rahul

9/28/2008 11:22:00 AM

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
6 Answers

joseph cook

9/28/2008 11:59:00 AM

0

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

Juha Nieminen

9/28/2008 6:36:00 PM

0

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.

blargg.h4g

9/28/2008 7:34:00 PM

0

In article
<4d3ff307-177b-466d-941b-700ba2b2a0e7@q26g2000prq.googlegroups.com>, Rahul
<rahulsharma@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.

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

9/29/2008 8:13:00 AM

0

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

James Kanze

9/29/2008 1:42:00 PM

0

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.

--
James Kanze (GABI Software) email:james.kanze@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

Rifat Albayrak

12/28/2012 10:35:00 AM

0

On Fri, 28 Dec 2012 11:16:13 +0100, "Acephale Lemar"
<privat@org.invalid> wrote:

>Choro'ya muhalif oldugum husus da bu: Ona g?re Almanca Ingilizce'yi
>etkilemistir, bana g?re ise tam tersi.

Bana g?re ?ngilizce Almanca'n?n bir koludur.