non-const reference and const reference

This is a discussion on non-const reference and const reference within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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,
    George

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    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
    Posts
    6,677
    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, 09:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10: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

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