March 1st, 2003, 04:22 AM
|
#1 (permalink)
| | Member
Join Date: Mar 2002 Location: BYU - Provo
Posts: 43
| Pass by Value/Reference
All right, I went to class today and we learned about pass by value and pass by reference. Here's some C++ code:
void f( int b) {
b=7;
}
int main(void) {
f(2+3);
}
Ok, this will compile fine. Now, how is this different?:
void f( int &b) {
b=7;
}
int main(void) {
f(2+3);
}
The first will compile fine. The second will give a warning. I'm just looking for some insight 
__________________
-Fractile81
|
| |
March 1st, 2003, 05:06 AM
|
#2 (permalink)
| | Moderator
Join Date: Oct 2001 Location: Winter Park FL
Posts: 5,278
|
Lets see if I understand what you are doing.
Looks like you have two functions. F and Main I dont know what you are doing with main, you decalre it an Int so you have to return a int or Return 0. but in the () you put void... Im not sure why. In your first way of doing it when you set B=7 it will only stay seven in that function and will return to the original value when the function is compleated but I think you understand that. In the second it will hold the new value till it is changed again. Can you post the actuall warning message?
-: phenious :- |
| |
March 1st, 2003, 03:19 PM
|
#3 (permalink)
| | Member
Join Date: Feb 2002
Posts: 161
|
Ok, the compiler will replace 2 + 3 with 5. Thus, you are sending the value 5 to a function which expects a reference to an integer. This can't be done. Why not? Because a reference is an alias for an object, but the value 5 isn't an object. It has no address. It is a constant integer literal, and it cannot be converted to an integer reference.
In the first case, the function f() is expecting an integer, not a reference to an integer. 5 can be converted to an integer. |
| |
March 2nd, 2003, 02:13 AM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Calgary, AB Canada
Posts: 754
|
when passing by reference that is a direct change meaning the value of the variable you are passing in the main function can be changed outside of main.
so when you pass by reference the compiler is expecting an integer variable to be passed and in this case you are passing a constant integer literal which is why this is incorrect like Martee stated.
In this case the compiler just passes it by value which is why you are getting the warning because the pass by reference is not needed in this case. |
| |
March 2nd, 2003, 02:26 AM
|
#5 (permalink)
| | Senior Member
Join Date: Aug 2002 Location: Meeshigan
Posts: 602
|
Exactly what Martee said. Here are examples using your code to illustrate:
void f(int b)
{
b=7;
}
int main(void)
{
int x;
x = 2 + 3;
f(x);
//x will hold the value 5, unchanged from before you called the function since you passed the value in x instead of the variable x to the function
}
void f(int &b)
{
b=7;
}
int main(void)
{
int x;
x = 2+3;
f(x);
//In this case, x will be 7 after returning from the function, as you passed a reference to x that holds the value, meaning when you change the value in the function, it affects the variable in the calling function as well.
}
*edit- what jaida said too*
__________________
About 5% of the people in the world can't think.
Another 5% can think and do.
The remaining 90% can think, but don't.
Last edited by Ruler2112 : March 2nd, 2003 at 02:41 AM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |