A
asit
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 ?
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 ?