Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 04-21-2006, 03:42 AM   #1
Beta Member
 
Join Date: Apr 2006
Posts: 5
Default Returning a Reference in C++

I don't understand the second line of this code.

int& max(int& m, int& n)
{return ( m > n ? m : n )
}

int main()
{ int m = 44, n = 22;
cout<< m << ", " << n<< ", " << max(m,n)<< endl;
max(m,n)= 55;
cout<< m << ", " << n<< ", " << max(m,n)<< endl;
}

Also, what is the point to return the value with references?

Thanks in advance,
Sasuke12
__________________

Sasuke12 is offline   Reply With Quote
Old 04-21-2006, 04:03 AM   #2
Golden Master
 
ArrizX's Avatar
 
Join Date: Apr 2005
Posts: 16,073
Send a message via MSN to ArrizX
Default Re: Returning a Reference in C++

There is something that has to do with C++ here..
http://computerforums.org/showthread.php?t=35435

I hope it helps.
__________________

__________________
. ()()()()
./l ,[_\_\ ],
l---L ()lllllll()-
()_) ()_)--o-)_)
ArrizX is offline   Reply With Quote
Old 04-21-2006, 04:36 AM   #3
Beta Member
 
Join Date: Apr 2006
Posts: 5
Default Re: Returning a Reference in C++

Yeah, I read through the cplusplus web site. Really haven't seen something as strange as the second line. I don't understand (m>n ? m:n), simply what does it mean? Or can some one tell me what category does that belong to, so I can check it up myself.

And I assume that I have to use reference because they are using variables in a separate function, correct me please?
Sasuke12 is offline   Reply With Quote
Old 04-21-2006, 07:16 AM   #4
C--
Solid State Member
 
C--'s Avatar
 
Join Date: Jan 2006
Posts: 8
Default Re: Returning a Reference in C++

Okay, I might be wrong, but I believe the int& max function is being given two variables to use, and the function returns the larger variable with a conditional statement. If m > n is true, the code returns m, the variable before the colon. If m > n is false, the code returns n, because that means n > m. You can read the statement like so:

expression ? if true do this : if not true do this

Because the command happens to be return, it will return the selected variable. As for references, normally when you pass data from function to function, you don't pass the actual variable, but a copy. When you pass by reference, you pass the memory address of the original variable, rather than a copy, so you actually work with the original variable itself. This allows a function to make direct changes to variables in another function, and to return values of more than one variable at a time.

If you would like more information, go to http://www.cplusplus.com/doc/tutorial/. Conditional statements are located under "Operators," and references are locted under "Functions (II)"
C-- is offline   Reply With Quote
Old 04-21-2006, 07:39 AM   #5
Daemon Poster
 
uid=[0]'s Avatar
 
Join Date: Apr 2006
Posts: 906
Send a message via Yahoo to uid=[0]
Default Re: Returning a Reference in C++

Quote:
Originally Posted by C--
Okay, I might be wrong, but I believe the int& max function is being given two variables to use, and the function returns the larger variable with a conditional statement. If M > n is true, the code returns m, the variable before the colon. If m > N is false, the code returns n, because that means n > m. You can read the statement like so:

expression ? if true do this : if not true do this

Because the command happens to be return, it will return the selected variable. As for references, normally when you pass data from function to function, you don't pass the actual variable, but a copy. When you pass by reference, you pass the memory address of the original variable, rather than a copy, so you actually work with the original variable itself. This allows a function to make direct changes to variables in another function, and to return values of more than one variable at a time.

If you would like more information, go to http://www.cplusplus.com/doc/tutorial/. Conditional statements are located under "Operators," and references are locted under "Functions (II)"

I would have to agree.
__________________
"Security is nothing more than a thought that makes you sleep well at night." - Me
MCSE/MCSA
Security+/Network+
Wireless Network Security Spec.
uid=[0] is offline   Reply With Quote
Old 04-21-2006, 06:19 PM   #6
Beta Member
 
Join Date: Apr 2006
Posts: 5
Default Re: Returning a Reference in C++

Thanks, that completely solved my problems.
__________________

Sasuke12 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 03:36 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0