home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2727
Discussions: 188,384, Posts: 2,243,508, Members: 232,615
Old March 1st, 2003, 04:22 AM   Digg it!   #1 (permalink)
Member
 
Join Date: Mar 2002
Location: BYU - Provo
Posts: 43
Send a message via AIM to Fractile81 Send a message via Yahoo to Fractile81
Question
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

Fractile81 is offline   Reply With Quote
Old March 1st, 2003, 05:06 AM     #2 (permalink)
Moderator
 
phenious's Avatar
 
Join Date: Oct 2001
Location: Winter Park FL
Posts: 5,278
Send a message via AIM to phenious
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 :-
__________________

phenious is offline   Reply With Quote
Old 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.

Martee is offline   Reply With Quote
Old March 2nd, 2003, 02:13 AM     #4 (permalink)
Senior Member
 
jaida's Avatar
 
Join Date: Oct 2001
Location: Calgary, AB Canada
Posts: 754
Send a message via ICQ to jaida
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.
jaida is offline   Reply With Quote
Old 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.
Ruler2112 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

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

Most Active Discussions
Is It Just Me? (2896)
CPU wont boot (6)
3-days in and no threads about Gaza (160)
The United States Debt (20)
I think I just killed my computer w.. (24)
hp compaq nc6000 problems (139)
Upgrading RAM (5)
Folderchat Weekday thread (442)
Antec 300 bulk purchase? (11)
Recent Discussions
CPU wont boot (6)
GLaDOS is up. (3)
HP notebook reinstall Vista NO .. (5)
Building a gaming computer advi.. (4)
hp compaq nc6000 problems (139)
Folderchat Weekday thread (442)
Creative T-3000 Subwoofer (3)
ACPI controller halt on boot (2)
Worth the upgrade?? (15)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 10:23 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28