|
-
October 31st, 2007, 02:35 PM
#1
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.
Programmer in the making...
-
October 31st, 2007, 05:31 PM
#2
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
-
October 31st, 2007, 05:39 PM
#3
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.
-
October 31st, 2007, 06:02 PM
#4
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 31st, 2007, 10:14 PM
#5
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.
-
November 1st, 2007, 02:54 PM
#6
Re: Pointers, passing by reference, and good 'style'
 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.
-
November 1st, 2007, 09:57 PM
#7
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.
Programmer in the making...
-
November 1st, 2007, 10:09 PM
#8
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.
-
November 2nd, 2007, 03:09 AM
#9
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.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
November 2nd, 2007, 03:48 PM
#10
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.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 3rd, 2007, 08:44 PM
#11
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 :-)
Please rate my post if you felt it was helpful
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|