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.
# 1 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
# 2 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:
void swapnum_bypointer(int *i, int *j) {
int temp = *i;
*i=*j;
*j=temp;
}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.
# 3 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.
# 4 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.
# 5 Re: Pointers, passing by reference, and good style
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.
hspc at 2007-11-9 1:30:20 >

# 6 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.
# 7 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.
# 8 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.
Kheun at 2007-11-9 1:33:21 >
