delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/18/12:30:00

From: Prashant Ramachandra <rprash AT wilco-int DOT com>
To: "'djgpp AT delorie DOT com'" <djgpp AT delorie DOT com>
Message-ID: <5BF60CD649EDD411A04600B0D049F53AFFA913@hydmail02.hyd.wilco-int.com>
Subject: [OT] About const int
Date: Wed, 18 Jul 2001 21:41:11 +0530
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2650.21)
Reply-To: djgpp AT delorie DOT com

I have a question that's probably got nothing to with DJGPP, so please
forgive me for this off-topic post.

I tried the program below and interestingly, it gives me...

"Constant is 10. It is actually 1"

Shouldn't gcc not make the assumption that the value of j *is* 10 and
instead reference itfrom memory instead.

> Program:
> 
> #include <iostream.h>
> 
> class Test {
> public:
>   static const int i;
> 
>   void run () { cout << i << endl; }
>   Test () { }
> };
> 
> const int Test::i = 7;
> 
> const int j = 10;
> 
> int main ()
> {
>   Test t1;
>   Test t2;
> 
>   int *k = (int *)&j;
>   *k = 1;
>   cout << "Constant is " << j << ". It is actually " << *k << endl;
>   t1.run ();
>   t2.run ();
> }
> 
Thanks a lot for any explanations.

Regards,
Prashant
--
Prashant TR <tr AT midpec DOT com>  Web: http://www.midpec.com/

"Those who do not understand Unix are condemned to reinvent it, poorly."
     -- Henry Spencer


This message is confidential and may also be legally privileged.  If you are not the intended recipient, please notify us immediately.  You should not copy it or use it for any purpose, not disclose its contents to any other person.  The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of Wilco International

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019