const conersions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    #1

    const conersions

    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

    #2
    Re: const conersions

    On Sep 28, 7:21 am, Rahul <rahulsha...@lu cent.comwrote:
    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.

    Comment

    • blargg

      #3
      Re: const conersions

      In article
      <4d3ff307-177b-466d-941b-700ba2b2a0e7@q2 6g2000prq.googl egroups.com>, Rahul
      <rahulsharma@lu cent.comwrote:
      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.

      Comment

      • James Kanze

        #4
        Re: const conersions

        On Sep 28, 1:21 pm, Rahul <rahulsha...@lu cent.comwrote:
        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.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Juha Nieminen

          #5
          Re: const conersions

          joseph cook wrote:
          On Sep 28, 7:21 am, Rahul <rahulsha...@lu cent.comwrote:
          >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.

          Comment

          • James Kanze

            #6
            Re: const conersions

            On Sep 28, 8:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
            joseph cook wrote:
            On Sep 28, 7:21 am, Rahul <rahulsha...@lu cent.comwrote:
            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.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...