What is the preferred way? The tutorial of this site mentions only the "int&"-kind, while I've seen the second kind used extensively in e.g. the DirectX-libraries from MS. I don't expect anyone to be inside the heads of the developers of DirectX, but do you know why they've used the more cumbersome "int*"-kind? It's both more elaborate to call, not to mention to implement.
Could it be that they've tried to accomodate C-programmers as well? Thanks!
int* is a pointer and therefore passing a pointer is called passing by pointer, not by reference.
You're right. One of the reasons is that C doesn't have references.
The other is that pointers can be modified. Though if you want to modify a pointer in a function you either have to pass an int** or int*&.
Thanks hamsterman, now I got my labels right! That helps clearing things up conceptually. Btw: what do you mean by "pointers can be modified"? I'm pretty new to this. Could you provide me with an example, please?
My understanding is that const reference parameters are prefered. In cases were arguments are going to be modified by the function which to choose is debatable. References can be operated on in the same manor as a copied object while pointers use a slightly different syntax (depending on the operations). References can not be null nor reassigned while pointers do not carry these restrictions. I have also heard that some prefer pointers because the & serves as a clue to the user that a call might/is changing the argument(s).
int my_array[5];
int* ptr = &my_array[0];//now ptr points to my_array[0]
ptr++;//now ptr points to my_array[1]
This isn't very commonly used though.
The things moorecm mentioned are good reasons to use pointers once in a while. Especially the ability to set it to 0. You may want your pointer not to point at anything at all. References can't do that.
IMO:
const references for variables you aren't changing
for other variables:
use pointers if you want the user to be able to pass "nothing" (NULL)
use references if you want to force the user to pass something
Especially the ability to set it to 0. [snip] References can't do that.
Which one might argue is reason to use references.
References are less likely to be null or "bad". Whereas null/bad pointers are all too common.
Of course there's nothing stopping you from making a bad/null reference... it's just less likely to happen by accident.
I tend to lean towards the following:
- I avoid passing "out" parameters (ie: references/pointers which are changed by the function). The need for them doesn't really come up as often as some people might thingk. But when necessary:
-- I use references when modifying a single object.
-- I use pointers when modifying an array of objects.
moorecm & firedraco: I concur. Did a little research, and I do believe const reference is my new favorite! It has the speed and ease of use of references, as well as protecting data.
hamsterman: thanks for the example.
Disch: thanks for sharing your thoughts and practices. In particular, your rule of thumb using references for modifying a single object and pointers for modifying an array of objects, resonate well with me.