// RefMatrix.h copyright (C) 2007 adolfo@di-mare.com /** \file RefMatrix.h \brief Muestra cómo implementar una matriz usando referencias \c lkptr. \author Adolfo Di Mare \date 2007 - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04 */ #ifndef RefMatrix_h #define RefMatrix_h #include // assert() #include "lkptr.h" // javify #include "Matrix.h" /// Definido por la biblioteca C++ estándar namespace std {} // Está acá para que Doxygen lo documente /// Matriz chirrisquitica de adolfo@di-mare.com namespace Mx { /// Empaque alrededor de \c Matrix que usa \c lkptr para mejorar la implementación de los operadores aritméticos. template class RefMatrix : public Mx::Matrix { public: RefMatrix(unsigned m = 1, unsigned n = 1) : Matrix(m,n) { } ///< Constructor base RefMatrix(const RefMatrix& o) : Matrix(o) { } ///< Constructor de copia RefMatrix(const E& V) : Matrix(V) { } ///< Construye una matriz unitaria ~RefMatrix() {}///< Destructor public: /// Retorna un puntero a una copia de la instancia. RefMatrix* clone() const { return new RefMatrix(*this); } friend lkptr operator + (const lkptr& A, const lkptr& B) { lkptr Res ( A->clone() ); Res->add(*B); return Res; } ///< Retorna \c A+B friend lkptr operator - (const lkptr& A, const lkptr& B) { lkptr Res ( A->clone() ); Res->substract(*B); return Res; } ///< Retorna \c A-B friend lkptr operator * (const lkptr& A, const lkptr& B) { RefMatrix *Res = new RefMatrix( A->rows(), B->cols() ); Res->multiply(*A, *B); return lkptr( Res ); } ///< Retorna \c A*B #if 0 { // Hay que usar: [ V->at(i,j) ] o [ (*V)(i,i) ] PERO... lkptr< RefMatrix > rV ( new RefMatrix(M,N) ); RefMatrix& VV = *rV; // VV(i,j) == (*V)(i,j) // ... for (i=0; i < rV->rows(); ++i) { for (j=0; j < rV->cols(); ++j,++k) { assert( rV->at(i,j) == VV(i,j) ); assert( &rV->at(i,j) == &VV(i,j) ); } } } #else // friend E& operator()( lkptr L, unsigned i, unsigned j ); // { return (*L)(i,j); } // Error: operator()() must be a nonstatic member function #endif }; // RefMatrix } // namespace Mx #endif // RefMatrix_h // EOF: RefMatrix.h