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; I am not sure if there's a good and easy way to ACTUALLY change the const value... When it turns ...

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I am not sure if there's a good and easy way to ACTUALLY change the const value...
    When it turns out I'd need to do that, I would delete the const keyword from the declaration...

    You put it there yourself to help compiler find errors in the code, so why would you want to bypass it?

  2. #17
    Registered User kishore's Avatar
    Join Date
    Jan 2007
    Location
    bangalore
    Posts
    6
    It works on TurboC++ 3.0
    which uses a temprory variable if the types of the variable and its reference don't match...........

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by kishore
    It works on TurboC++ 3.0
    which uses a temprory variable if the types of the variable and its reference don't match...........
    .....and is practically guaranteed to allow subtle bugs in the program!?

    If you have a const, and you try to modify its reference, then you are making a logical error. It's a good thing compilers can catch this.

    I use const references very often to pass large classes to functions that shouldn't modify them. If the compiler allowed me to modify the object, that would mean that the function could be seriously flawed without anybody noticing it.
    Last edited by anon; 01-18-2007 at 09:29 AM.

  4. #19
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    that's actually undefined behaviour according to the standard
    Quote Originally Posted by section 7.1.5.1
    Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const
    object during its lifetime (3.8) results in undefined behavior.
    Code:
    const int* ciq = new const int (3); // initialized as required
    int* iq = const_cast<int*>(ciq); // cast required
    *iq = 4; // undefined: modifies a const object
    this means that while it might work on your particular compiler, another compiler is free to put const values into rom and crash horribly when you try to write to it. Actually undefined means it can actually do anything! (sending porn to your boss, deleting your hard disk, these are perfectly acceptable in the eyes of the standard! )

    bottom line: don't use const_cast to modify a const pointer or reference unless you're damn sure that whatever it points to isn't const!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Page 2 of 2 FirstFirst 12
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, 12:45 PM
  2. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 04:13 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 12:02 PM

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