Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-09-2009
Member
 
Join Date: Aug 2009
Posts: 76
Problem of const variable and its reference

I am having problem with my const variable. When I create a non-constant reference to this variable, the problem arises is that the two variables have different values although the address remaining same.

Code:
#include <iostream.h>
#include <conio.h>
int main()
{
    clrscr();
    const int k = 4; 
    int& p = const_cast<int&>(k);
    p++;
    cout << "k = " << k << "\t&k = " << &k << endl
         << "p = " << p << "\t&p = " << &p << endl;
    getch();
}
Code Output:

Quote:
k = 10 &k = 0xbfffca88
p = 11 &p = 0xbfffca88
Which is wrong. A single address cannot store 2 different values, if I am not wrong then. Then what is wrong in my code?
Reply With Quote
  #2  
Old 04-09-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Problem of const variable and its reference

I don't see any problem with your code but I am assuming that we can't reference the constant variable. May be it is not allowed. This is the reason, probably, which caused you in undefined behavior. I think you should avoid this kind of statements in your code. And by the way why you want to have 2 variables referencing to the same static value, you can even use the same variable names where that value is required.
Reply With Quote
  #3  
Old 04-09-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Problem of const variable and its reference

Quote:
int& p = const_cast<int&>(k);
I think the problem lies over here. Basically before this statement, the compiler was assuming the value of k as 10 which is not the case. It has already be changed to 11 as per your code. So during run-time, the k is still displayed as 10 (due to compiler's confusion) and p value is changed to 11.
Reply With Quote
  #4  
Old 04-09-2009
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: Problem of const variable and its reference

This is because although you have given the reference of k to p, p is still not a constant variable and so the value of p can be changed at any point of time. On the contrary, k remains constant throughout the code and so cannot be changed. That is the reason why you are getting the value of k as 10 and the value of p as 11 (which got changed in your code).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem of const variable and its reference"
Thread Thread Starter Forum Replies Last Post
Circular Reference Problem timpatmc Microsoft Project 2 30-04-2011 03:01 AM
How to declare const variable in C++ AZUL Software Development 3 02-09-2009 06:36 PM
Runtime Error 91 : Object Variable or with block variable not set Ryan21 Software Development 2 28-08-2009 07:51 PM
Php variable increment problem Rilex Software Development 3 06-05-2009 07:39 PM
const Obj problem in C++ Archetype Software Development 2 25-10-2008 05:58 PM


All times are GMT +5.5. The time now is 02:53 PM.