Re: Reference parameters
"jeffc" <nobody@nowhere .com> wrote:
[color=blue]
> Rather than calling it more C++ish, I'd say it's actually more readable[/color]
and[color=blue]
> clear, and less error prone than using pointers. Why use all that nasty
> pointer syntax inside the doStuff function, when simply using the[/color]
parameter[color=blue]
> name naked is much more clear?[/color]
One way around this would be to use a local reference for readability:
void fred(int *x_ptr)
{
assert(x_ptr != NULL);
int &x = *x_ptr;
... do something with x ...
}
That way the caller can say fred(&x) and the function can still be readable,
too.
I learnt Pascal before C/C++, and it had a useful "with" statement which let
you reference fields of a record (struct) in the next block without needing
to use the full name; using a local reference is a bit like that. I do this
using constant references when I don't want to write something out in full
each time:
const SomeStructType &s = someArrayOfArra ys[n].someArrayOfStr ucts[m];
cout << s.field1 << ", " << s.field2;
David F
"jeffc" <nobody@nowhere .com> wrote:
[color=blue]
> Rather than calling it more C++ish, I'd say it's actually more readable[/color]
and[color=blue]
> clear, and less error prone than using pointers. Why use all that nasty
> pointer syntax inside the doStuff function, when simply using the[/color]
parameter[color=blue]
> name naked is much more clear?[/color]
One way around this would be to use a local reference for readability:
void fred(int *x_ptr)
{
assert(x_ptr != NULL);
int &x = *x_ptr;
... do something with x ...
}
That way the caller can say fred(&x) and the function can still be readable,
too.
I learnt Pascal before C/C++, and it had a useful "with" statement which let
you reference fields of a record (struct) in the next block without needing
to use the full name; using a local reference is a bit like that. I do this
using constant references when I don't want to write something out in full
each time:
const SomeStructType &s = someArrayOfArra ys[n].someArrayOfStr ucts[m];
cout << s.field1 << ", " << s.field2;
David F
Comment