Re: problem with accessing a "struct" using "->"
On 29 Mar, 13:20, "arnuld" <geek.arn...@gmail.com> wrote:
On Mar 29, 4:37 pm, "Gavin Deane" <deane_ga...@hotmail.com> wrote:
Glad you got it working.
:-)
You might know this, but note that there is
no need for ref_jd in your main function. This variation has identical
effect:
i did not know this.
int main()
{
address jd;
fill_addr(jd);
print_addr(jd);
return 0;
}
yes, it runs.
Gavin, it means, it is an "implicit conversion" to a "reference".
isn't using "explicit references" a good coding practice ?
There is no conversion. References are not objects in their own right.
A reference simply gives you an alias for an existing object. Whenever
you use the name of the reference in code, it is as if you had used
the name of the object referred to. So when you do fill_addr(jd) in
main, the reference-to-address parameter of the fill_addr function is
initialised to refer to the object jd.
In your original code you had this in your main function
address jd; // an "address" struct
address& ref_jd = jd; // <<-- LINE 2
fill_addr(ref_jd); // <<-- LINE 3
On the line I have marked as Line 2, ref_jd is initialised to refer to
the object jd. From then on, wherever you write ref_jd, it is as if
you had written jd. So on the line I have marked as Line 3, what is
the reference-to-address parameter of the fill_addr function
initialised to refer to now? It doesn't refer to ref_jd. That's
meaningless. ref_jd isn't an object - it isn't a thing that can be
referred to. ref_jd is simply an alias for jd. There is no such thing
as a reference to a reference. A reference has to refer to some
object, and the object in question here is jd. So the reference-to-
address parameter of the fill_addr function is initialised to refer to
the object jd. Exactly as before.
ref_jd is entirely redundant and serves only to clutter your code and
potentially confuse a reader.
Gavin Deane