In "The C++ Programming Language (4th Edition)" Section 2.3.1, Stroustrup shows 3 different ways to access members of a struct
:
void f(Vector v, Vector& rv, Vector* pv)
{
int i1 = v.sz; // access through name
int i2 = rv.sz; // access through reference
int i4 = pv->sz; // access through pointer
}
- I understand that for the first one,
v
is passed-by-value, so a copy of the first argument is put on the function's stack and the value of its sizesz
is stored inside ofi1
. - In the second example,
rv
is a reference to the struct passed as the second argument. Because it is a reference, we can access the value referred to byrv
without a*
prefix.
I'm not too sure I understand what's going on with i4
, and why someone would pick the third example over the second (or vice-versa).