1

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
}
  1. 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 size sz is stored inside of i1.
  2. 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 by rv 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).

user2864740
  • 60,010
  • 15
  • 145
  • 220
jborica
  • 11
  • 1

0 Answers0