By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,736 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

different between reference and value

P: n/a
what's the differnce between the following two functions:

void func(int &t){
}

void fun(int t){
}

Can I define both of them as member functions of a class?
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
David wrote:
what's the differnce between the following two functions:

void func(int &t){
}
This one operates the on `t' itself; `t' can be changed.

void fun(int t){
}
This one operates on a copy of `t'.
Can I define both of them as member functions of a class?


Sure. Why not?

HTH,
--ag
--
Artie Gold -- Austin, Texas

20050120->44
Jul 22 '05 #2

P: n/a

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2p************@uni-berlin.de...
David wrote:
what's the differnce between the following two functions:

void func(int &t){
}


This one operates the on `t' itself; `t' can be changed.

void fun(int t){
}


This one operates on a copy of `t'.


Actually, t is the local variable itself, isnt it?

In the first instance, t is simply a reference to the variable that was
passed to func, and it is actually the variable that was passed to func that
is being operated on inside the function. In the second case, t is a local
copy of the variable that was passed to fun, and operations on it do not
directly affect the variable that was passed to the function, they only
affect the local copy (which is destroyed upon exit from the function).

-Hhoward
Jul 22 '05 #3

P: n/a
In article <8d**************************@posting.google.com >,
hu*****@yahoo.com (David) wrote:
what's the differnce between the following two functions:

void func(int &t){ t = 5; }

void fun(int t){ t = 5; }
int main() {
int a = 0;
int b = 0;
func( a );
assert( a == 5 );
fun( b );
assert( b == 0 );
// see the difference now?
}
Can I define both of them as member functions of a class?


Yes.
Jul 22 '05 #4

P: n/a
Howard wrote:
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:2p************@uni-berlin.de...
David wrote:
what's the differnce between the following two functions:

void func(int &t){
}


This one operates the on `t' itself; `t' can be changed.

void fun(int t){
}


This one operates on a copy of `t'.

Actually, t is the local variable itself, isnt it?

In the first instance, t is simply a reference to the variable that was
passed to func, and it is actually the variable that was passed to func that
is being operated on inside the function. In the second case, t is a local
copy of the variable that was passed to fun, and operations on it do not
directly affect the variable that was passed to the function, they only
affect the local copy (which is destroyed upon exit from the function).

-Hhoward

You have expressed it better than I did.

!
--ag

--
Artie Gold -- Austin, Texas

20050120->44
Jul 22 '05 #5

P: n/a

"David" <hu*****@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...
what's the differnce between the following two functions:

void func(int &t){
}

void fun(int t){
}

Can I define both of them as member functions of a class?


You mean func not fun?

You can define both as a member of a class, but as soon as you try to call
one you are going to get a ambiguous method call error. I'm assuming that
you meant 'func' not 'fun' as in your previous post.

john
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.