A non-const reference may only be bound to an lvalue?

This is a discussion on A non-const reference may only be bound to an lvalue? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am debugging MSDN code from, http://msdn2.microsoft.com/en-us/lib...ah(VS.80).aspx here is my output, 1>main.cpp 1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) : warning C4350: ...

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

    A non-const reference may only be bound to an lvalue?

    Hello everyone,


    I am debugging MSDN code from,

    http://msdn2.microsoft.com/en-us/lib...ah(VS.80).aspx

    here is my output,

    1>main.cpp
    1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) : warning C4350: behavior change: 'B::B(A)' called instead of 'B::B(B &)'
    1> d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(13) : see declaration of 'B::B'
    1> d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(9) : see declaration of 'B::B'
    1> A non-const reference may only be bound to an lvalue

    I do not quite understand why there is a warning A non-const reference may only be bound to an lvalue and 'B::B(A)' called instead of 'B::B(B &)'? Could anyone give some descriptions please?


    thanks in advance,
    George

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The site you got the code from is the explanation why this warning appears, it's the example code for reproducing it. I'll try paraphrasing it:

    In compiler version 2002, if the compiler had the choice if it would transform an object returned by a function to a non-const-reference or another type, it would have chosen the non-const-reference. 2005 and better will instead opt to cast the value to another type that is passed either by value or by const reference instead. Why ? Well, passing a value that will vanish anyway to a function that will write to that value makes no sense. No one will ever see the results.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    In the poster's favor though Microsoft does come up with some fairly cryptic error messages along with equally cryptic explanations in their help files.

    Today we loaded code in C# and the form designer siad it could not load. The error message was long and cryptic and left all of us developers sitting there scratching our heads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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