john townsley said:
are there any differences when using pointers or passing by reference when
using
1) basic variables
2)complex variable types like arrays,structures
it seems easier to simply pass by reference and simply forget about pointers
but thats seems to easy, there must still be a need for pointers
For the most obvious implementations of pointers and references, performance
is unlikely to be affected. However, a pointer can be null, but a reference
can't. Sometimes you might want to use a pointer so you can pass a null
pointer. Pointers can also be re-seated, but references can't. However,
pointers passed to functions usually aren't re-seated. Pointers can result
in uglier, more verbose code than references because a pointer needs to be
dereferenced, sometimes many times. You might also want to consider the call
to the function:
f(&anObject); // pointer
f(anObject); // reference
In the pointer case it's obvious from the call that an address is being
passed, and therefore that the function might change 'anObject' (if the
pointer is not to const). In the reference case you can't tell from the call
whether it's pass by value ('anObject' has to copied and cannot be changed)
or pass by reference (it is not copied and can be changed).
DW