Thread: non-const reference and const reference

  1. #1
    Registered User
    Join Date
    May 2006

    non-const reference and const reference

    Hello everyone,

    This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks.

    1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.

    2. A non-const reference can not binded to a rvalue, I think the reason is rvalue is not addressable? And we can not change the rvalue through its reference? Are there any other reasons? I am not quite sure whether my understanding is fully correct. Since there are some non-modifiable lvalues (so we do not always need to modify values through its reference). I am still studying what is the reason in essence in compiler why a non-const reference can not be binded to a rvalue.

    3. Both const and non-const reference can be binded to a lvalue.

    thanks in advance,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    A const reference can be bound to a temporary object.
    A non-const reference cannot be bound to a temporary object (according to the standard, but Visual Studio allows this).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    1) The life of a temporary object has no relationship to the lifetime of any reference to that object. Hence the notion of a dangling reference (akin to a dangling pointer) which is essentially a reference to an object that no longer exists.

    2) The address of an rvalue can be computed (which, I guess, means it is "addressable" to use your language). An rvalue (roughly) is something that appears on the right hand side of an assignment expression and, typically, is not something that needs to be changed. Assignment involves an "rvalue to lvalue conversion" which, with the expression "a = b" means converting a copy of the value of b (the rvalue) so it can be stored into a (the lvalue). That conversion does not (generally) change b but it changes a, so b is treated as const, the value obtained from b (from the lvalue to rvalue conversion) is treated as const, but a can be (so can receive either a const or a non-const).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM