const_cast

Discussion in 'C++' started by Podrzut_z_Laweczki, Jul 31, 2006.

  1. Hello everyone, I have a question:

    const double zaq = 1.23;
    const double *ptcd = &zaq;
    double *ptd;
    ptd = const_cast<double*>(ptcd);
    *ptd = 5.67;
    cout<<zaq<<endl;

    It's only an exercise. I tried that in VS.NET 2003 and it seems to work
    only with floating point types. I know it's not the best way, but I'm
    just curious why this example doesn't work for other types.
     
    Podrzut_z_Laweczki, Jul 31, 2006
    #1
    1. Advertising

  2. Podrzut_z_Laweczki

    Howard Guest

    "Podrzut_z_Laweczki" <> wrote in message
    news:...
    > Hello everyone, I have a question:
    >
    > const double zaq = 1.23;
    > const double *ptcd = &zaq;
    > double *ptd;
    > ptd = const_cast<double*>(ptcd);
    > *ptd = 5.67;
    > cout<<zaq<<endl;
    >
    > It's only an exercise. I tried that in VS.NET 2003 and it seems to work
    > only with floating point types. I know it's not the best way, but I'm
    > just curious why this example doesn't work for other types.
    >


    In what way does it "work"? It's not that this "isn't the best way", but
    that it's just plain wrong. Using a trick to allow it to happen doesn't
    make it legal. It's not. It is Undefined Behavior to change something
    which you've declared as const. (Undefined Behavior means it may work, or
    it may not; it may crash, or it may not; it _may_ do just about anything.)

    -Howard
     
    Howard, Jul 31, 2006
    #2
    1. Advertising

  3. Podrzut_z_Laweczki

    Noah Roberts Guest

    Podrzut_z_Laweczki wrote:
    > Hello everyone, I have a question:
    >
    > const double zaq = 1.23;
    > const double *ptcd = &zaq;
    > double *ptd;
    > ptd = const_cast<double*>(ptcd);
    > *ptd = 5.67;
    > cout<<zaq<<endl;
    >
    > It's only an exercise. I tried that in VS.NET 2003 and it seems to work
    > only with floating point types. I know it's not the best way, but I'm
    > just curious why this example doesn't work for other types.


    Modification of a const object is always undefined. The behavior of
    such undefined behavior is thus often times rather unpredictable and
    undefined.
     
    Noah Roberts, Jul 31, 2006
    #3
  4. Howard wrote:
    > In what way does it "work"? It's not that this "isn't the best way", but
    > that it's just plain wrong. Using a trick to allow it to happen doesn't
    > make it legal. It's not. It is Undefined Behavior to change something
    > which you've declared as const. (Undefined Behavior means it may work, or
    > it may not; it may crash, or it may not; it _may_ do just about anything.)


    I didn't intend to use that, but I get the picture now. Thank you.
     
    Podrzut_z_Laweczki, Jul 31, 2006
    #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:
    447
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

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

    const_cast<>

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

    const_cast

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

    const_cast question

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

Share This Page