what is the difference ? (pass by reference)



 DEVELOP > c-Plus-Plus > what is the difference ? (pass by reference)

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Vasileios"
Date: 27 Nov 2003 06:52:34 AM
Object: what is the difference ? (pass by reference)
Hello,
could someone tell me what is the difference between:
1)
int *data;
void doSomething(*data)
{
}
2)
int data;
void doSomething(&data)
{
}
they both pass data by reference right?
Or not?
Thank you
Vasileios
.

User: "friedrich"

Title: Re: what is the difference ? (pass by reference) 29 Nov 2003 08:50:38 AM

1)

int *data;

void doSomething(*data)

This should be:
void doSomething(int *data)

2)
int data;
void doSomething(&data)

This should be:
void doSomething(int &data)
In the first example you would have to use a pointer-to-int as the
function argument, in the second, you could use an int as an argument
and the function would automatically use a reference. If you used
const:
void doSomething(const int &data)
that would protect the int from being changed outside of the function.
Your function name looks like a Java style name. Are you a Java
programmer?
-FA
.

User: "Jon Bell"

Title: Re: what is the difference ? (pass by reference) 27 Nov 2003 09:04:25 AM
In article <40d3a3ec.0311270452.13ce1851@posting.google.com>,
Vasileios <vasileios@zografos.org> wrote:


1)

int *data;

void doSomething(*data)
{

}



2)
int data;
void doSomething(&data)
{

}



they both pass data by reference right?
Or not?

Not. They're both invalid syntax.
--
Jon Bell <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.

User: "Nils Petter Vaskinn"

Title: Re: what is the difference ? (pass by reference) 27 Nov 2003 07:11:02 AM
On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:

void doSomething(*data)
void doSomething(&data)
they both pass data by reference right?

No, one passes data by reference the other passes a reference to the data.
--
NPV
"the large print giveth, and the small print taketh away"
Tom Waits - Step right up
.
User: "Vasileios"

Title: Re: what is the difference ? (pass by reference) 28 Nov 2003 03:59:16 AM
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message news:<pan.2003.11.27.13.08.39.250657@spam.for.me.invalid>...

On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:


void doSomething(*data)
void doSomething(&data)


they both pass data by reference right?


No, one passes data by reference the other passes a reference to the data.

Hmm... does this mean that with the first "void doSomething(*data)"
any changes I make inside the function will change the data that is
passed from outside, and the second "void doSomething(&data)" will
only change the data inside the function?
V.Z.
.
User: "Dave OHearn"

Title: Re: what is the difference ? (pass by reference) 28 Nov 2003 08:08:46 PM
(Vasileios) wrote:

Hmm... does this mean that with the first "void doSomething(*data)"
any changes I make inside the function will change the data that is
passed from outside, and the second "void doSomething(&data)" will
only change the data inside the function?

It would help if you gave some info on how long you have been using
C++. Are you familiar with pointers and trying to understand how
references are different, or are you new to all of this stuff at once?
--
Dave O'Hearn
.

User: "Kon Tantos"

Title: Re: what is the difference ? (pass by reference) 29 Nov 2003 01:34:49 PM
Vasileios wrote:

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message news:<pan.2003.11.27.13.08.39.250657@spam.for.me.invalid>...

On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:



void doSomething(*data)

this passes a copy of a reference (called a pointer). Your function can change 'data'
without affecting the copy in the calling code. You can dereference 'data' to change
data in the calling code (*data = ...)

void doSomething(&data)

this passes a reference to data in the calling code. When you access data you are
_actually_ working with the outside data. So data = ... directly changes whatever
data is 'refering to'.




they both pass data by reference right?


No, one passes data by reference the other passes a reference to the data.




Hmm... does this mean that with the first "void doSomething(*data)"
any changes I make inside the function will change the data that is
passed from outside, and the second "void doSomething(&data)" will
only change the data inside the function?


V.Z.

.
User: "Vasileios Zografos"

Title: Re: what is the difference ? (pass by reference) 29 Nov 2003 03:25:15 PM
Kon Tantos wrote:



Vasileios wrote:

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:<pan.2003.11.27.13.08.39.250657@spam.for.me.invalid>...

On Thu, 27 Nov 2003 04:52:34 -0800, Vasileios wrote:



void doSomething(*data)


this passes a copy of a reference (called a pointer). Your function can
change 'data' without affecting the copy in the calling code. You can
dereference 'data' to change data in the calling code (*data = ...)

void doSomething(&data)


this passes a reference to data in the calling code. When you access
data you are _actually_ working with the outside data. So data = ...
directly changes whatever data is 'refering to'.




they both pass data by reference right?



No, one passes data by reference the other passes a reference to the
data.





Hmm... does this mean that with the first "void doSomething(*data)"
any changes I make inside the function will change the data that is
passed from outside, and the second "void doSomething(&data)" will
only change the data inside the function?


V.Z.



excellent. thats what I wanted to know.
plain and simple.
Thank you
.




User: "Grumble"

Title: Re: what is the difference ? (pass by reference) 27 Nov 2003 08:36:46 AM
Vasileios wrote:

could someone tell me what is the difference between:

Does this help?
http://www.parashift.com/c++-faq-lite/references.html
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER