Pointers, passing by reference, and good 'style'
I was reading on pointers in a PDF I found on the tubes, and I noticed that a code example uses pointers to do a pass by reference.
In C++ I last spring, when we wanted to pass a value by reference, we used int& someVar; for the variable in the parameter. The PDF uses int* someVar; instead. As far as I know, they do the same thing with no extra abilities given / taken from each one. Which is the more common, or preferred method?
Also, are pointers just like reference variables but involving two named variables (pointer, pointee), and not constricted to a parameter, or is there more?
M.
Re: Pointers, passing by reference, and good 'style'
Pointers and references are two different things. Conceptually a pointer is a memory location holding a pointer to the area of memory that holds the data you require. A reference is the memory location of the data you require.
There are instances where both are valid, however int *someVar is passing a pointer to a valuea nd int &someVar is passing a value by reference. I suppose the difference would be that when passing a pointer you can change the address to which that pointer points where as when passing a reference you can only change the values associated with that memory address.
Hope that helps.
Cheers
Odd
Re: Pointers, passing by reference, and good 'style'
the other difference is how you change the value. e.g. in a very basic function that swaps two numbers:
Code:
void swapnum_bypointer(int *i, int *j) {
int temp = *i;
*i=*j;
*j=temp;
}
Code:
void swapnum_byreference(int &i, int &j) {
int temp = i;
i=j;
j=temp;
}
if you use i instead if *i in the first function you will alter the address, not the value as stated above.
Re: Pointers, passing by reference, and good 'style'
Plus, a pointer can legally have the value 0 (pointing to nothing). A reference must refer to some valid entity.
Re: Pointers, passing by reference, and good 'style'
The main benefit of passing pointers rather than references is that it makes it much clearer which parameters are inputs and which are outputs.
In fact, the only time I used pass-by-reference I also use const (if at all possible given controllable code), just to make it plain that the thing isn't an output. I only do it to avoid unnecessary copies on potentially bulky objects.
Re: Pointers, passing by reference, and good 'style'
Quote:
Originally Posted by GuOddian
I suppose the difference would be that when passing a pointer you can change the address to which that pointer points where as when passing a reference you can only change the values associated with that memory address.
Yes, but note that changing the value of the pointer (he address to which that pointer points) will not reflect in the calling function because the pointer itself is passed by value.
Re: Pointers, passing by reference, and good 'style'
I see. So I mixed that up? The way it sounded was that it essentialy achieves the same goal of being able to change data without a return _____;.
M.
Re: Pointers, passing by reference, and good 'style'
No, you're right about that. You can change the data the pointer points at. You merely can't change the pointer itself.
Which is why God invented pointers to pointers.
Re: Pointers, passing by reference, and good 'style'
In short, you can say that reference to equivalent to a constant pointer other than the syntax used is much neater. :)
BTW, people with background in C and haven't fully understand references, tend to use pointer. Without declaring the pointer as constant, the compiler will not be able to flag out error when the pointer is unintentionally modified.
Re: Pointers, passing by reference, and good 'style'
Just to point this out: References are a C++ concept, so if the pdf you've been reading is about C and not C++, it would have to use pointers to pass a variable by reference.
Re: Pointers, passing by reference, and good 'style'
IMO, pass-by-reference and pass-by-pointer accomplish the same thing. Mainly, to be able to modify/use some method parameter without copying it so that the changes are reflected in the calling code.
The difference to me is that reference notation is much, much, much cleaner and neater to use. But that could just be the C++ side of me talking :-)