const_cast

Discussion in 'C++' started by Comp1597@yahoo.co.uk, May 3, 2009.

  1. Guest

    I've been told that const_cast "adds or removes the constness of an
    expression".
    I understand that it converts pointers to const to pointers to non-
    const and references to const to references to non-const. But how
    does a const_cast affect a variable of type const int* const? I would
    assume a const_cast would make such a variable have type const int*
    Is this correct?

    Thanks
     
    , May 3, 2009
    #1
    1. Advertising

  2. Guest

    On May 3, 9:36 pm, wrote:
    > I've been told that const_cast "adds or removes the constness of an
    > expression".
    > I understand that it converts pointers to const to pointers to non-
    > const and references to const to references to non-const.  But how
    > does a const_cast affect a variable of type const int* const?  I would
    > assume a const_cast would make such a variable have type const int*
    > Is this correct?
    >
    > Thanks


    Sorrmy I meant that const_cast transforms the type const int* const to
    int* const. Is this correct?
     
    , May 3, 2009
    #2
    1. Advertising

  3. Neelesh Guest

    On May 4, 1:17 am, wrote:
    > On May 3, 9:36 pm, wrote:
    >
    > > I've been told that const_cast "adds or removes the constness of an
    > > expression".
    > > I understand that it converts pointers to const to pointers to non-
    > > const and references to const to references to non-const.  But how
    > > does a const_cast affect a variable of type const int* const?  I would
    > > assume a const_cast would make such a variable have type const int*
    > > Is this correct?

    >
    > > Thanks

    >
    > Sorrmy I meant that const_cast transforms the type const int* const to
    > int* const.  Is this correct?


    The following const_cast will transform a variable of type const int*
    const to int* const
    (It removes constness of the data).
    const int i = 10;
    const int* const r = &i;
    int* const r2 = const_cast<int* const>(r);

    However, since the actual data (i) is originally cosnt, any attempt to
    change the value of i through r2 will result in undefined behavior.


    The following const_cast will tranfrom a variable of type const int*
    const to const int*
    (It removes constness of the pointer)

    const int i = 10;
    const int* const r = &i;
    const int* r3 = const_cast<const int*> (r);

    Note that the const_cast is actually redundant here, since we are
    removing "top level" constness for a pointer, which is anyway legal as
    we are doing a copy. Had r3 been a reference, then you would need an
    explicit const_cast:

    const int*& r3 = r; //Error
    const int*& r3 = const_cast<const int*&> (r); //Works.
     
    Neelesh, May 4, 2009
    #3
  4. James Kanze Guest

    On May 3, 9:36 pm, wrote:
    > I've been told that const_cast "adds or removes the constness of an
    > expression".
    > I understand that it converts pointers to const to pointers to
    > non- const and references to const to references to non-const.
    > But how does a const_cast affect a variable of type const int*
    > const? I would assume a const_cast would make such a variable
    > have type const int* Is this correct?


    Maybe. It depends on the cast itself. Note however that the
    results of a const_cast (or any cast not returning a reference
    type) is an rvalue, so top level const has no meaning. (It
    would have meaning if the results were a class type, but
    const_cast can never return a class type.) So even if you
    const_cast< int const* const >, the results will in fact be int
    const*.

    --
    James Kanze (GABI Software) email:
    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, May 4, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    427
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    486
    Josephine Schafer
    Aug 4, 2003
  3. R. Anbeeswaran

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    607
    Ekkehard Morgenstern
    Nov 14, 2003
  4. S.Senthilvel

    const_cast

    S.Senthilvel, Jan 6, 2004, in forum: C++
    Replies:
    4
    Views:
    2,038
    Andrey Tarasevich
    Jan 8, 2004
  5. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    551
    Bill Seurer
    May 5, 2004
Loading...

Share This Page