-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
  • 47,390
  • 120
  • 299
  • 507

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,720
  • 11
  • 21