Modifying reference to a const

This is a discussion on Modifying reference to a const within the C++ Programming forums, part of the General Programming Boards category; Will tis program work............. Itried to compile it with g++ and it didn't work Code: #include<iostream.h> int main() { const ...

  1. #1
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6

    Question Modifying reference to a const

    Will tis program work.............
    Itried to compile it with g++ and it didn't work

    Code:
    #include<iostream.h>
    int main()
    {
    	const int i = 10;
    	int &j = i;
    
    	cout<<"i = "<<i<<"j = "<<j;
    
    	j = 20;
    
    	cout<<"i = "<<i<<"j = "<<j;
    
    	return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    so why do you want to modify the const?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    What errors were you getting?

    also, use <iostream>

  4. #4
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6
    Its in a book which says it will work and the out put is 10 10 10 20

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Its in a book which says it will work and the out put is 10 10 10 20
    The attempt to initialise an int& with a const int looks illegal to me. What book is that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by kishore
    Its in a book which says it will work and the out put is 10 10 10 20
    That is impossible. That would suggest that i and j occupy seperate memory space, which they do not because j is a reference to i. (an illegal one at the moment though, because j would have to be a reference to const int)

    This would be completely correct if the ampersand was removed.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Dave_Sinkula
    1. They use cast removing const. In the example above there is no cast.
    2. The value after modification is the same - no matter how they retrieve it though the pointer or through the var itself. In the description abouve the value retrieved through the reference is different from the velue retrieved through the var itself.

    Both these issues make me doubt that the example can be compiled and run with the described results
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't const more like a keyword that lets the compiler find errors such as this?
    Code:
    const int a = 42;
    int b = 100;
    if (a = b)

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Yes, const tells the compiler that the value returned will not be changed.
    I'm just trying to be a better person - My Name Is Earl

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    That's one of it's uses, anon.

    In this case, the code started with a const int i (which the compiler must view as having a value that can't change), and attempted to create a non-const reference to it (which the compiler is required to reject because it would allow changing a value that can't change). Converting a constant into a non-constant is not something the compiler will allow voluntarily.

    The example in the link given by Dave is a little different, as vart said. Apart from the minor fact it manipulated a pointer and dereferenced it (rather than playing with a reference), it used an explicit cast to bludgeon the compiler into submission -- so it forced the compiler to allow removal of the "const" qualifier.

    If a book has claimed that the code quoted by kishore should print 10 10 10 20, then that book is badly broken. The code should not have even compiled. If a cast had been used to force the compiler into submission. For example;
    Code:
    #include <iostream>
    int main()
    {
    	const int i = 10;
    	int &j = (int &)i;   //  explicit conversion here
    
    	std::cout<<"i = "<<i<<"j = "<<j;
    
    	j = 20;
    
    	std::cout<<"i = "<<i<<"j = "<<j;
    
    	return 0;
    }
    it will technically yield undefined behaviour (which is why the conversion was disallowed in the first place, and the original code should not have compiled). However, if it was just to print 4 values, it would print 10 10 20 20.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Based on the syntax of the OP's code snippet, perhaps the idea of the writers was within the previous C++ standard before they were changed. I do know one of the standard changes was to leave the trailing .h from all headers excluding windows.h. I do not know for sure that the const issue debated was a standard change. Perhaps somone can help clear this book error up.
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    The meaning of const, and its affect on references, was established quite well, early on. The meaning has not changed much since the ARM (Annotated Reference Manual) which was the book written by Stroustrup and Eliis to guide the (then) proposed development of the standard. About the only appreciable change I recall in the standardisation process was tightening up the requirement that a reference must be initialised so it was a reference to something.

    The naming of standard headers without the .h came fairly late (about 95), but only affected those headers which were specifically part of the C++ standard. It did not affect the headers inherited from C (stdio.h, etc), but usage of those was deprecated in the C++ standard. It had no effect on proprietary headers such as windows.h.

    My bet would be that the book being used by kishore was one who's author did not understand what references were about -- in particular that, unlike pointers, they cannot be reseated. In the early 90's there were a number of texts that demonstrated that sort of misunderstanding by their authors.

  14. #14
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6
    I threw away that book today.........

    it was named "Let Us C++" by Yeshwank Kanitkar
    the guy who wrote "Let Us C"
    this guy uses TurboC++ and BorlandC++
    he is a well known writer of books on C & C++ in INDIA


    thank you guys for your valuable comments and suggestions......

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This outputs what your book said (maybe it was in your book because the author wanted to show that even this doesn't work for const variables):
    Code:
    #include <iostream>
    int main(){
        const int i = 10;
        int *j = (int*)&i;
        std::cout<<"i = "<<i<<"j = "<<*j;
        *j = 20;
        std::cout<<"i = "<<i<<"j = "<<*j;
        std::cin.get();
        return 0;
    }
    I am not sure if there's a good and easy way to ACTUALLY change the const value...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 4
    Last Post: 01-23-2008, 05:21 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21