Re: How to return a reference, when I really need it
BlackLight wrote:
Vector Matrix::operator[] (size_t i) throw() {
if (i >= rows)
throw InvalidMatrixIndexException();
vector<float> row;
for (int j=0; j < cols; j++)
row.push_back(matrix[i][j]);
return Vector(row);
}
Not an answer to your question, but I would like to note that your
function there is really, really heavy.
First of all, allocating a vector every time that function is called
is going to be a rather heavy operation, especially for the intended
purpose.
Secondly, even if you simply *must* do that and there's no way around
it, at the very least *reserve* the necessary capacity in the vector
before filling it. That way you will completely avoid reallocations,
which is an even heavier operation, and can potentially happen several
times during that loop. (It will also lessen memory fragmentation.)
Thirdly, learn to use the handy functions each STL container offers. I
don't know how your 'matrix' member is implemented, but you will
probably be able to do it like this:
row.assign(matrix[i], matrix[i] + cols);
or like this:
row.assign(matrix[i].begin(), matrix[i].end());
depending on what kind of data container that 'matrix' is.
Not that this will be relevant in your final version of that function,
but just wanted to comment.