Reference parameters
From: David Fisher (nospam_at_nospam.nospam.nospam)
Date: 11/30/03
- Next message: P.J. Plauger: "Re: Dinkumware / Comeau / DMC??"
- Previous message: muser: "Re: can this be achieved"
- Next in thread: Thore Karlsen: "Re: Reference parameters"
- Reply: Thore Karlsen: "Re: Reference parameters"
- Reply: Julián Albo: "Re: Reference parameters"
- Reply: Andrew Koenig: "Re: Reference parameters"
- Reply: Jon Bell: "Re: Reference parameters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 Nov 2003 17:54:29 +1100
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 ...
- Next message: P.J. Plauger: "Re: Dinkumware / Comeau / DMC??"
- Previous message: muser: "Re: can this be achieved"
- Next in thread: Thore Karlsen: "Re: Reference parameters"
- Reply: Thore Karlsen: "Re: Reference parameters"
- Reply: Julián Albo: "Re: Reference parameters"
- Reply: Andrew Koenig: "Re: Reference parameters"
- Reply: Jon Bell: "Re: Reference parameters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|