GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2004, 23:20
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road

Passing referance and passing pointer


Will anyone explain to me the advantage of one over the other - Passing the reference and passing the poniter as parameter in function calls. The sittutions where i need to use one over the other.
  #2  
Old 28-Oct-2004, 03:39
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
are both Passing the reference and passing the pointer as parameter is the same? did you mean passing parameter by reference and by value?
  #3  
Old 28-Oct-2004, 03:47
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
No, Imeant passing reference and passing pointer
  #4  
Old 28-Oct-2004, 07:26
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Poolan.

I have to agree with if's assessment. However, I know that C++ has perverted the semantics of reference passing, so this may have something to do with C++, if so you can completely disregard my answer...

I like to look at this a bit differently. You either pass an address or a value. If you pass an address, your function will be working on the data that is at that address. If you pass a value, your function will make a copy of the data and any changes made will be made to the copy, the original will be untouched.

As far as I am concerned, these are the only two ways of passing parameters in any language regardless of the semantics of that language.

Here is a small sample:

CPP / C++ / C Code:
void change_me(int *data)   //Pass by pointer (reference)
{
  *data = 5;
}

void dont_change(int data)  //Pass by value
{
  data = 5;
}

int main()
{
  int number = 2;

  printf("Number is: %d\n",number);
  dont_change(number);
  printf("Number is: %d\n",number);
  change_me(&number);
  printf("Number is: %d\n",number);

  return 0;
}

The output from the above program is:
Code:
Number is: 2 Number is: 2 Number is: 5

Hopefully this helps a little.
  #5  
Old 28-Oct-2004, 22:00
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
Hi Smith,

I agree wit you smith, but I am talking about C++. It provides both passing pointers and references. And there is some advantage of one over the other.

Thanks for your reply.
  #6  
Old 29-Oct-2004, 05:22
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Poolan
Hi Smith,

I agree wit you smith, but I am talking about C++. It provides both passing pointers and references. And there is some advantage of one over the other.

Thanks for your reply.

I thought that's what you meant!! (didn't reply 'cause I wasn't sure)
The advantage of passing by reference would be that you can use the passed parameter in the function more naturally.
Observe:

CPP / C++ / C Code:
void FuncByPointer(dataType * data)
{
	*data = whatever;
	data->DoSomething();
}

void FuncByReference(dataType & data)
{
	data = whatever;
	data.DoSomething();
}

However, you can (in the body of the function) change the address the pointer points to. You can't do that with references! They are aliases for your data (const pointers, if you want).

I don't know of any other advantage/disadvantage. Maybe someone else...

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #7  
Old 29-Oct-2004, 07:18
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Thanks Luci! This has actually come up before in this forum and it was the first time that I ever heard of it. Being an old C guy that just kind of dabbles in C++, I was shocked and amazed to here about this...

My comment to this:

CPP / C++ / C Code:
void FuncByReference(dataType & data)
{
	data = whatever;
	data.DoSomething();
}

is AAAAAAHHHHHHH!!!!

Thats just me though. It smacks against everything I know about C. Just my 2 cents...
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not 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
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.