469,645 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Can anyone explain this??

Creative
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Chris | last post: by
13 posts views Thread by C++fan | last post: by
reply views Thread by Alan Silver | last post: by
33 posts views Thread by O-('' Q) | last post: by
19 posts views Thread by Alan Silver | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.