469,645 Members | 1,698 Online

Can anyone explain this??

Returning (by) Reference

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

"A function call,min(x,y)will 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?
Jan 6 '07 #1
1 949
horace1
1,510 Expert 1GB
Returning (by) Reference

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

"A function call,min(x,y)will 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.
Jan 6 '07 #2