-3

If I pass a vector by reference to a function:

void QuadraticInterpolateArray(vector<double> &b) {
    double step = 12.2;
    for (int j = 0; j < b.size(); j++) {
        b[j] = step;
    }
}

I don't need later to deference b when I need to access to its value by using operator []. Instead, if I pass vector by pointer:

void QuadraticInterpolateArray(vector<double> *b)

I need (*b)[j] later.

In both case I'm passing the "address" of that vector. Why with reference it works and with pointer doesn't? Its just by design?

markzzz
  • 45,272
  • 113
  • 282
  • 475

1 Answers1

1

Because references are automatically dereferenced by the compiler.

This was introduced by C++ to simplify C's pointers.

And yes, this is a design choice.

Mattia F.
  • 1,660
  • 9
  • 20