Results 1 to 4 of 4

Thread: Problem of const variable and its reference

  1. #1
    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:

    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?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem of const variable and its reference

    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.

  4. #4
    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).

Similar Threads

  1. Circular Reference Problem
    By timpatmc in forum Microsoft Project
    Replies: 2
    Last Post: 30-04-2011, 03:01 AM
  2. How to declare const variable in C++
    By AZUL in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 06:36 PM
  3. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  4. Php variable increment problem
    By Rilex in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 07:39 PM
  5. const Obj problem in C++
    By Archetype in forum Software Development
    Replies: 2
    Last Post: 25-10-2008, 05:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,543,180.87650 seconds with 17 queries