I have to create a Matrix class, and I got some problem to overload the operators
I would like to fill up a matrices using the << operator
Matrix<double> u3(2,2);
u3 << 3.3, 4, 3, 6;
template<class T>
Matrix<T> Matrix<T>::operator <<(T in){
//Fill up the matrix, m[0] = 3.3, m[1]=4...
return *this;
}
How overload this operator?
Here’s an approach using commas:
Demo: http://ideone.com/W75LaH
Some explanations:
The syntax
matrix << 5, 10, 15, 20is achieved in two steps:matrix << 5is evaluated first; it sets the first element to 5 and returns a temporaryAdderobject that handles further insertions (remembering the index for the next insert)Adderhas overloadedoperator,that performs the following inserts after each comma.