Please don't top-post. Thank you. Rearranged.
Ivan said:
Thanks for the reply.
So the efficiency is as good as passing a reference? If so why pass
reference?
There is a fundamental difference between passing references to objects
and passing pointers to objects. A reference must always refer to a
valid object. A pointer may refer to a valid object or may be null.
Therefore, if a function accepts a pointer, it must accept the
possibility of a null pointer and check the pointer value before using
it. Change the function to accept a reference and this extra
responsibility is removed. The function can safely assume that an
object is there.
The only time this is not an advantage is, of course, when, in your
design, the possibility of an object not existing makes sense. A
reference is no use here because it always referes to a valid object.
Pass a pointer and the function can query that pointer to see if it is
null or not.
A reference says: Here is the object for you to work with.
A pointer says: Here is an indicator that tells you whether the
optional object exists and, if so, where it is.
Summary: use references when you can and pointers when you must.
Gavin Deane