Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   const_cast issue (http://www.velocityreviews.com/forums/t806021-const_cast-issue.html)

asit 11-17-2011 10:57 AM

const_cast issue
 
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 ?

Goran 11-17-2011 12:17 PM

Re: const_cast issue
 
On Nov 17, 11:57*am, asit <lipu...@gmail.com> 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.

Richard Damon 11-19-2011 05:22 PM

Re: const_cast issue
 
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).


All times are GMT. The time now is 11:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57