Re: references and pointers
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:5p4rqvFp7vuvU11@mid.individual.net...
borophyll@gmail.com wrote:
On Nov 4, 12:00 pm, Ian Collins <ian-n...@hotmail.com> wrote:
boroph...@gmail.com wrote:
Why then does Stroustrup make the following statement in his book:
"Be suspicious of non-const reference arguments; if you want the
function to modify its arguments, use pointers and value return
instead"
He made a mistake? Or possibly idiomatic style has changed over time.
*Please* don't quote signatures.
Are you serious? This is in the most recent version of "The C++
programming language", and he mentions this point in several
locations. Hardly an editing mistake. Let me just state that I am
not debating that the point he makes seems right, it does not seem
right to me, but it seems to be more than just a mistake.
http://www.research.att.com/~bs/bs_faq2.html#pointers-and-references
Puts a different view across, so one of my guesses was probably true.
I'd only recommend using pointers when the argument can meaningfully be
NULL, or when interfacing with legacy code.
I was actually debating this with myself 2 days ago. Someone gave me some
code work on, he was using pointers to modify the parameters. I started to
change them to references, then realized that in mainline, there is no
indication if the parameter was going to be changed or not.
Consider.
void foo( int* Val )
{
*Val = 23;
}
void bar( int& Val )
{
Val = 23;
}
int main()
{
int MyInt = 10;
foo( &MyInt );
bar( MyInt );
}
Becaue Foo forces us to take the address of MyInt, it is fairly obvious in
mainline that MyInt is probably going to be changed, else why pass the
address of a simple int? bar however gives no indication in mainline that
MyInt will be changed.
Maybe that is the reasoning behind it.
Reguardless, I did go ahead and change them all to references anyway.