D
David Fisher
I have been encouraged by someone else to use a reference rather than a
pointer as an "out" parameter to a function, eg.
void doStuff(Thing &out1, Thing &out2)
as opposed to:
void doStuff(Thing *out1, Thing *out2)
The issue I have with this is that when the function is called, you cannot
tell that the value is being modified:
doStuff(thing1, thing2);
This is much less obvious than:
doStuff(&thing1, &thing2);
The only disadvantages I can think of with using pointers here are:
- a NULL pointer could be passed in by mistake
- the body of doStuff() is slightly more verbose (*out1 instead of out1, ->
instead of ".")
- I suppose the pointer could be accidentally reassigned, incremented,
deleted, etc
I would rank readability and clarity in the use of functions as more
important than any of these things (since most of a program's lifetime is
usually maintenance, not necessarily by the original writer). The friend who
was encouraging the use of references said that it was "more C++ish", but
that by itself isn't a very good argument ...
Any comments ?
David F
PS. I am aware of
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6 ...
pointer as an "out" parameter to a function, eg.
void doStuff(Thing &out1, Thing &out2)
as opposed to:
void doStuff(Thing *out1, Thing *out2)
The issue I have with this is that when the function is called, you cannot
tell that the value is being modified:
doStuff(thing1, thing2);
This is much less obvious than:
doStuff(&thing1, &thing2);
The only disadvantages I can think of with using pointers here are:
- a NULL pointer could be passed in by mistake
- the body of doStuff() is slightly more verbose (*out1 instead of out1, ->
instead of ".")
- I suppose the pointer could be accidentally reassigned, incremented,
deleted, etc
I would rank readability and clarity in the use of functions as more
important than any of these things (since most of a program's lifetime is
usually maintenance, not necessarily by the original writer). The friend who
was encouraging the use of references said that it was "more C++ish", but
that by itself isn't a very good argument ...
Any comments ?
David F
PS. I am aware of
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6 ...