Can anyone explain this??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Creative
    New Member
    • Jan 2007
    • 14

    Can anyone explain this??

    Returning (by) Reference

    Consider the following function:
    float & min(float&a,flo at&b)
    {
    if(a>b)
    return a;
    else
    return b;
    }

    "A function call,min(x,y)wi ll yield a reference to either a or b depending upon which one is lesser than the two.This means that this function can appear on the left hand side of an assignment statement since it returns a reference to a variable.That is,
    min(x,y) = -5;
    assigns -5 to the lesser of the two,x&y
    ."


    What does this mean?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Creative
    Returning (by) Reference

    Consider the following function:
    float & min(float&a,flo at&b)
    {
    if(a>b)
    return a;
    else
    return b;
    }

    "A function call,min(x,y)wi ll yield a reference to either a or b depending upon which one is lesser than the two.This means that this function can appear on the left hand side of an assignment statement since it returns a reference to a variable.That is,
    min(x,y) = -5;
    assigns -5 to the lesser of the two,x&y
    ."


    What does this mean?
    A reference is an alias for an object, i.e. it holds the address of an object,. It can therefore be used as an lvalue (appear on the left side of an assignment statement).
    Note that the function actually returns a reference to the maximum parameter not the minimum.

    Comment

    Working...