"Andrew Bell [4036]" wrote: > > On Sun, Mar 14, 1999 at 05:12:13AM +0000, Glynn Clements wrote: > > > holotko wrote: > > > > In C++ when passing values to a function we can pass values by > > > "reference" like so: > > > > > > int foo(int& int&); // prototype for foo() > > > > > > main() > > > { > > > > > > int x = 10; > > > int y = 20; > > > int result; > > > > > > result = foo(x, y); // The values of x and y passed to > > > > > > } > > > > > > And foo() defined as: > > > > > > int foo(int& a, int& b) > > > { > > > int value; > > > ... > > > return value; > > > } > > > > > > One possible advantage to passing values this way is that in addition > > > to returning a value to main() side effects are produced,i.e. if foo() > > > modifies a or b the change will be reflected in the values of x or y > > > in main, if such effects are desired. > > > > > > Pretty much the same effect can be easilly acheived via passing > > > arguments as pointers (which is how I normally go about such a thing). > > > My main 2 questions are: > > > > > > 1) What is the overall advantage of passing arguments this way (by > > > reference), as oppsoed to using pointers , and, > > > None. The above code is equivalent to having `foo' declared as > > > int foo(int* a, int* b); > > > and using `*a' and `*b' in the body. C++ references are merely > > `syntactic sugar' for using pointers. Personally I dislike this > > feature, as it makes it harder to see what is really going on. > > Isn't there at least one distinction? I don't have my C++ references > handy (in fact they're on a remote island in South-Western Canada..), > but passing by reference always meant that the object referred to by > the reference existed. Pointers could be NULL (ie. point to NULL or > invalid memory), but references needed to refer to instantiated objects > of their type. > > Anyhow, it really is stupid to have both. Either pass EVERYTHING by > reference and include a NULL object (that other objects inherit from), > or do it the C way. > > Andrew Bell > [EMAIL PROTECTED] Personally I think I prefer the overall versatility of pointers, even though they can be dangerous and can sometimes lead to fatal errors. My only problem with pointers is that sometimes I tend to overuse them, or at least I have been told that I do. Perhaps the "passing by reference" idea was an attempt to make C++ safer than C, in the sense that one can pass by reference under certain circumstances and avoid pointers and the potential problems which accompany them, unlike Java, which eliminated the pointer entirely. -- email: [EMAIL PROTECTED] Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
begin:vcard n:Holotko;John x-mozilla-html:FALSE org:MicroService Co. adr:;;;;;; version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer note;quoted-printable:Unix, Systems Programming, Applications, =0D=0ASoftware Develpment, Sytems Administration. x-mozilla-cpt:;0 fn:John Holotko end:vcard