Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11562

const_cast issue

From asit <lipun4u@gmail.com>
Newsgroups comp.lang.c++
Subject const_cast issue
Date 2011-11-17 02:57 -0800
Organization http://groups.google.com
Message-ID <30719501.757.1321527452906.JavaMail.geo-discussion-forums@prdy11> (permalink)

Show all headers | View raw


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 ?

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

const_cast issue asit <lipun4u@gmail.com> - 2011-11-17 02:57 -0800
  Re: const_cast issue Goran <goran.pusic@gmail.com> - 2011-11-17 04:17 -0800
  Re: const_cast issue Richard Damon <news.x.richarddamon@xoxy.net> - 2011-11-19 12:22 -0500

csiph-web