![]() |
|
#1
|
|||
|
|||
Passing referance and passing pointerWill 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
|
|||
|
|||
|
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
|
|||
|
|||
|
No, Imeant passing reference and passing pointer
|
|
#4
|
||||
|
||||
|
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:
The output from the above program is: Code:
Hopefully this helps a little. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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:
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
|
||||
|
||||
|
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:
is AAAAAAHHHHHHH!!!! Thats just me though. It smacks against everything I know about C. Just my 2 cents... __________________
The best damn Sports Blog period. |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The