const_cast

Discussion in 'C++' started by S.Senthilvel, Jan 6, 2004.

  1. S.Senthilvel

    S.Senthilvel Guest

    hi,
    I am a little confused about the const_cast.I've thought it this way till
    now.

    //Proper cast
    int i = 10;
    const int* pci = &i;

    int *pi = const_cast<int*>(pci);

    // Undefined cast
    const int ci = 10;
    const int* pci = &ci;

    int *pi = const_cast<int*>(pci);

    I thought that the result of a const_cast is undefined if the
    object pointed to is really a constant.(or is it that modifying that result
    is undefined)

    In "More Effective C++" , I saw a piece of code which goes this way

    class SpecialWidget;
    void update(SpecialWidget* psw);

    const SpecialWidget sw;
    update(const_cast<SpecialWidget*>(&sw));

    Here Meyers says that sw can now be safely updated inside "update" function.
    I fell this is contradictory to what i have understood.
    Can anyone kindly explain.

    Thanks,
    Senthilvel.
    S.Senthilvel, Jan 6, 2004
    #1
    1. Advertising

  2. S.Senthilvel

    Jack Klein Guest

    On Tue, 6 Jan 2004 09:24:42 +0530, "S.Senthilvel" <>
    wrote in comp.lang.c++:

    > hi,
    > I am a little confused about the const_cast.I've thought it this way till
    > now.
    >
    > //Proper cast
    > int i = 10;
    > const int* pci = &i;
    >
    > int *pi = const_cast<int*>(pci);
    >
    > // Undefined cast
    > const int ci = 10;
    > const int* pci = &ci;
    >
    > int *pi = const_cast<int*>(pci);
    >
    > I thought that the result of a const_cast is undefined if the
    > object pointed to is really a constant.(or is it that modifying that result
    > is undefined)


    No, the cast is fine. It is only modifying any object that was
    actually defined as const that is undefined.

    > In "More Effective C++" , I saw a piece of code which goes this way
    >
    > class SpecialWidget;
    > void update(SpecialWidget* psw);
    >
    > const SpecialWidget sw;
    > update(const_cast<SpecialWidget*>(&sw));
    >
    > Here Meyers says that sw can now be safely updated inside "update" function.
    > I fell this is contradictory to what i have understood.
    > Can anyone kindly explain.
    >
    > Thanks,
    > Senthilvel.


    If Meyers wrote this, he is just plain wrong. On some implementations
    with memory-management hardware, the processor may well generate a
    hardware trap and terminate the program. On an embedded system, const
    objects may be stored with the code in ROM or Flash memory devices
    that are literally not writable by normal programs, or at all, and
    there will be no trap but the value will remain unchanged. As far as
    the C++ standard is concerned, anything can happen if you try to
    modify an object actually defined as const.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 6, 2004
    #2
    1. Advertising

  3. S.Senthilvel wrote:
    > I am a little confused about the const_cast.I've thought it this way till
    > now.
    >
    > //Proper cast
    > int i = 10;
    > const int* pci = &i;
    >
    > int *pi = const_cast<int*>(pci);
    >
    > // Undefined cast
    > const int ci = 10;
    > const int* pci = &ci;
    >
    > int *pi = const_cast<int*>(pci);
    >
    > I thought that the result of a const_cast is undefined if the
    > object pointed to is really a constant.(or is it that modifying that result
    > is undefined)


    The result of 'const_cast' is defined. But an attempt to modify the
    constant object through the resultant pointer will result in undefined
    behavior.

    >
    > In "More Effective C++" , I saw a piece of code which goes this way
    >
    > class SpecialWidget;
    > void update(SpecialWidget* psw);
    >
    > const SpecialWidget sw;
    > update(const_cast<SpecialWidget*>(&sw));
    >
    > Here Meyers says that sw can now be safely updated inside "update" function.
    > I fell this is contradictory to what i have understood.
    > Can anyone kindly explain.
    > ...


    I took a look into my copy of "More Effective C++" and the closest thing
    I could find looked as follows (code from Item 13, modified for brevity):

    SpecialWidget sw;
    const SpecialWidget& csw = sw;
    update(const_cast<SpecialWidget*>(&csw));

    Note that this is not the same as your code. This code does not attempt
    to modify a constant object.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 6, 2004
    #3
  4. > I took a look into my copy of "More Effective C++" and the closest thing
    > I could find looked as follows (code from Item 13, modified for brevity):
    >
    > SpecialWidget sw;
    > const SpecialWidget& csw = sw;
    > update(const_cast<SpecialWidget*>(&csw));
    >
    > Note that this is not the same as your code. This code does not attempt
    > to modify a constant object.
    >

    Thanks for your answer Andery..

    But I do have the piece of code i've mentioned in "Item 2 : Prefer C++ style
    casts"

    on page 13 of my "More Effective C++".

    It may be that i have a old edition of it..I'm having a Fourth Indian
    Reprint, 2001.



    Best Regards,

    Senthilvel.


    > --
    > Best regards,
    > Andrey Tarasevich
    >
    Senthilvel Samatharman, Jan 8, 2004
    #4
  5. Senthilvel Samatharman wrote:
    >> I took a look into my copy of "More Effective C++" and the closest thing
    >> I could find looked as follows (code from Item 13, modified for brevity):
    >>
    >> SpecialWidget sw;
    >> const SpecialWidget& csw = sw;
    >> update(const_cast<SpecialWidget*>(&csw));
    >>
    >> Note that this is not the same as your code. This code does not attempt
    >> to modify a constant object.
    >>

    > Thanks for your answer Andery..
    >
    > But I do have the piece of code i've mentioned in "Item 2 : Prefer C++ style
    > casts"
    >
    > on page 13 of my "More Effective C++".
    >
    > It may be that i have a old edition of it..I'm having a Fourth Indian
    > Reprint, 2001.
    > ...


    Oh, my mistake. I was also referring to Item 2 in "More Effective C++"
    (I don't know where I got that "Item 13" from). I'm using CD version of
    the book, which is based, AFAIK, on the 1996 edition. It is strange that
    your edition is so different.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 8, 2004
    #5
    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:
    414
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

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

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    588
    Ekkehard Morgenstern
    Nov 14, 2003
  4. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    537
    Bill Seurer
    May 5, 2004
  5. Tobias Kilian
    Replies:
    2
    Views:
    1,491
    Michiel Salters
    May 25, 2004
Loading...

Share This Page