const_cast issue

Discussion in 'C++' started by asit, Nov 17, 2011.

  1. asit

    asit Guest

    Just go through the code

    1 #include <iostream>
    2 #include <cstdlib>
    3
    4 using namespace std;
    5
    6 double funct1(double& f)
    7 {
    8 f++;
    9 cout<<"f = "<<f<<endl;
    10 return f;
    11 }
    12
    13 void funct2(const double& d)
    14 {
    15 cout<<"d = "<<d<<endl;
    16 double value = funct1(const_cast<double&>(d));
    17 cout<<"value = "<<value<<endl;
    18 cout<<"d = "<<d<<endl;
    19 }
    20
    21 int main()
    22 {
    23 const double c = 4.324;
    24 funct2(c);
    25 double &k = const_cast<double&>(c);
    26 k++;
    27 cout<<"c = "<<c<<endl;
    28 cout<<"k = "<<k<<endl;
    29 return 0;
    30 }


    At line 27, value is printed as 4.324.

    But at line 28, value is printed as 6.324.

    Can you tell me why even though I am incrementing only once ?
    asit, Nov 17, 2011
    #1
    1. Advertising

  2. asit

    Goran Guest

    On Nov 17, 11:57 am, asit <> wrote:
    > Just go through the code
    >
    >   1 #include <iostream>
    >   2 #include <cstdlib>
    >   3
    >   4 using namespace std;
    >   5
    >   6 double funct1(double& f)
    >   7 {
    >   8         f++;
    >   9         cout<<"f = "<<f<<endl;
    >  10         return f;
    >  11 }
    >  12
    >  13 void funct2(const double& d)
    >  14 {
    >  15         cout<<"d = "<<d<<endl;
    >  16         double value = funct1(const_cast<double&>(d));
    >  17         cout<<"value = "<<value<<endl;
    >  18         cout<<"d = "<<d<<endl;
    >  19 }
    >  20
    >  21 int main()
    >  22 {
    >  23         const double c = 4.324;
    >  24         funct2(c);
    >  25         double &k = const_cast<double&>(c);
    >  26         k++;
    >  27         cout<<"c = "<<c<<endl;
    >  28         cout<<"k = "<<k<<endl;
    >  29         return 0;
    >  30 }
    >
    > At line 27, value is printed as 4.324.
    >
    > But at line 28, value is printed as 6.324.
    >
    > Can you tell me why even though I am incrementing only once ?


    Answer 1: nobody can tell you why because you tried to cast away a
    const from "c". Since C is a const variable, that's undefined behavior
    (in both C and C++). So you should be lucky that your computer didn't
    explode.

    Answer 2: you pass "c" by reference to funct2, who passes it by
    reference to funct1, who increments it once (line 8). You increment
    yourself it at line 27. That's 6.324. So you seem to have been lucky
    and this worked as if "c" wasn't const. But you can legally only use
    const_cast to cast const away with variables that weren't declared
    "const".

    Goran.
    Goran, Nov 17, 2011
    #2
    1. Advertising

  3. On 11/17/11 5:57 AM, asit wrote:
    > Just go through the code
    >
    > 1 #include <iostream>
    > 2 #include <cstdlib>
    > 3
    > 4 using namespace std;
    > 5
    > 6 double funct1(double& f)
    > 7 {
    > 8 f++;
    > 9 cout<<"f = "<<f<<endl;
    > 10 return f;
    > 11 }
    > 12
    > 13 void funct2(const double& d)
    > 14 {
    > 15 cout<<"d = "<<d<<endl;
    > 16 double value = funct1(const_cast<double&>(d));
    > 17 cout<<"value = "<<value<<endl;
    > 18 cout<<"d = "<<d<<endl;
    > 19 }
    > 20
    > 21 int main()
    > 22 {
    > 23 const double c = 4.324;
    > 24 funct2(c);
    > 25 double &k = const_cast<double&>(c);
    > 26 k++;
    > 27 cout<<"c = "<<c<<endl;
    > 28 cout<<"k = "<<k<<endl;
    > 29 return 0;
    > 30 }
    >
    >
    > At line 27, value is printed as 4.324.
    >
    > But at line 28, value is printed as 6.324.
    >
    > Can you tell me why even though I am incrementing only once ?


    Since c is declared const, the implementation is allowed to assume that
    it will not change, and thus when you reference it on line 27, the
    implementation is allowed to use the know value, and is not required to
    go back to the actually variable to get the value.

    By using const_cast to remove the const from an object that was declared
    const, you have invoked undefined behavior, which means in part, that
    the implementation may end up making assumptions that no longer hold,
    like that c has the value 4.324

    Undefined behavior means that anything might happen. In your case c has
    two different values that might be obtained depending on if the
    implementation decides to actually access the value is c or use what it
    "knows" it contains. Some other reasonably possible results are that c
    doesn't actually change, or that the program is aborted when you
    attempted to change the value. Technically, the implementation has been
    given by the C++ standard to do anything that it might want to do in
    this case, including very bad things like erasing your hard disk or
    making your computer turn plaid (of course other rules, like the laws of
    nature, may limit what it actually is able to do).
    Richard Damon, Nov 19, 2011
    #3
    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:
    412
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

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

    const_cast<>

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

    const_cast

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

    const_cast question

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

Share This Page