How to return a reference, when I really need it
Hi,
I'm actually developing a tiny C++ library for managing linear algebra
operations, but I have a problem in managing the reference to an
object as return value. This is the method I 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);
}
where Matrix is a class I wrote for matrices management, and Vector is
another class (fundamentally a wrapping around vector<float> I wrote
for doing cool mathematical tricks, like sums, scalar products, norms
etc.). This is the actual implementation, but I would like to return a
reference to the Vector object, i.e. Vector& Matrix::operator[] (...).
I need it to do tricks like
Matrix A;
A[0][0] = 1.0;
or something like that, and this is not possible returning the Vector
object as a value. But if I declare a local Vector object and I return
its reference, I've got a problem as returning the reference of a
local variable. Has any of you a solution for this?
Thanks,
BlackLight