#ifdef comment

What we would really like is for the Matrix class to return a smart
reference to allocated memory, something like this:

Ref Matrix::operator*(const Matrix& m1, const Matrix& m2)
{
     Matrix *m = new Matrix(rows, cols);
     /* multiply */
     return Ref(m);
}

Ref would store the pointer m and provide conversion functions to
Matrix and Matrix&; when the Ref returned went out of scope, it would
delete the memory.  So far, there is no problem.  However, really we
want to be able to specify any one of many difference smart reference
classes (e.g.: a copy-on-write reference class).  In other words, we
want to declare something like

template <class RefType> class Matrix {
     friend RefType operator*(const Matrix& m1, const Matrix& m2);
};

and then change the last line in the function above to be "return
RefType(m)".  The problem occurs in the definition of RefType.
Presumably, we will also want it to be a template, parameterized on
the type it is a smart reference to, like

template <class T> class Ref { ... };

We would like, then, to be able to declare a variable of type
Matrix<Ref<Matrix>> and have it work properly (and without copying out
returned objects).  Of course, this won't work because the innermost
mention of Matrix in that type is a template and therefore requires a
type parameter; so we end up with Matrix<Ref<Matrix<WHAT?>>>.

My solution to this problem is to rename the Matrix class to
RealMatrix and *not* make it into a template.  I then derive a class
template 

template <class Ref> class RefMatrix : public RealMatrix { ... };

that contains all the overloaded operators as previously defined in
the Matrix template.  Now, we can declare variables of type
RefMatrix<Ref<RealMatrix>>> and functions like

template <class RefType>
RefType RefMatrix<RefType>::operator*(const RefMatrix&, const RefMatrix&);

(Hmmm.. this function would have to work on variables of type
RealMatrix, RefMatrix, and RefType.  Probably RefMatrix and RefType
will have to provide the right conversion functions.

#endif /* comment */

/*
 * Class RealMatrix contains the actual code of the "Matrix" class --
 * it knows how to multiply, etc.  It contains no overloaded
 * operators, and all of its member functions are designed so as not
 * to need to copy out a result value.
 */
class RealMatrix {
     RealMatrix& Multiply(const RealMatrix&, const RealMatrix&, RealMatrix&);
};

/*
 * Class template RefMatrix is wrapper around RealMatrix.  It's whole
 * purpose in life is to provide overloaded operators for the
 * functions in RealMatrix.  Its argument type Ref is a reference
 * class (like RetVal) that can be initialized with a pointer,
 * converted to a RealMatrix, and will destroy the object it points
 * to when no longer used.
 */
template <class Ref> class RefMatrix : private RealMatrix {
     Ref operator*(const RefMatrix&, const RefMatrix&);
};

template <class Ref>
Ref RefMatrix<Ref>::operator*(const RefMatrix<Ref>& m1 ,
			      const RefMatrix<Ref>& m2)
{
     RealMatrix *m = new RealMatrix(rows, cols);

     Multiply(m1, m2, *m);
     // Copies only the pointer to a RealMatrix, not the RealMatrix.
     return Ref(*m);
}

/* For convenience, so we can refer to things as Matrix */
typedef RetVal<RealMatrix> RetMatrix 
typedef RefMatrix<RetMatrix> Matrix;

main()
{
     /* Calls RefMatrix<RetMatrix>::RefMatrix(int, int) which is */
     /* inhereted from RealMatrix::RealMatrix(int, int). */
     Matrix m1(2, 2);

     /* similarly */
     Matrix m2(2, 2, 1, 2, 3, 4);

     /* Calls m1.RefMatrix<RetMatrix>::Identify() which, again, has */
     /* been inhereted from RealMatrix.
     m1.Identify();

     /* Calls operator*(const RefMatrix<Ref>&, const RefMatrix<Ref>&) */
     /* which returns a RetVal<RealMatrix>.  Probably now it will */
     /* lose, since the compiler won't know to try RetVal<T>::operator */
     /* T&() in order to get something that has operator<< defined. */
     cout << m1 * m2;
}
