I have never use template before, so bear with me.
Here is what I am trying to do:
I declare an abstract base class MatrixInterface with template to
define an interface for all my subsequent Matrix class. In
MatrixInterface class, I overloaded the << operator by calling a pure
virtual function PrintDebugMessa ge(ostream &os); then I can implement
the function on individual Matrix classes later.
I create a Matrix class called SimpleMatrix, and implemented
PrintDebugMessa ge(). Everthing works great until I want to implement
different behavior depending whether it's
template<typena me DataTypevoid
SimpleMatrix<Da taType>::PrintD ebugMessage(... ); //this is fine
or
//error
template<typena me DataTypevoid SimpleMatrix< SimpleMatrix<Da taType>
>::PrintDebugMe ssage(...);I want to print out data differently if a instance of my SimpleMatrix
is a collection of sub-matrices of type SimpleMatrix. However, I can't
get that to work.
If I sepcialize the template parameter like:
// works, but now only when submatrices has type of SimpleMatrix<in t>
template<void SimpleMatrix< SimpleMatrix<in t>
>::PrintDebugMe ssage(...);then it works. How I generalize the function above to handle
SimpleMatrice submatrix of all type?
Below is a sample code of my problem. It compiles on gcc 3.2.3 and
intel c++ 8.1
*/
/*************** *************** **************
*************** *************** **************/
#include <iostream>
#include <typeinfo>
using namespace std;
/*************** *************** ******
** ABC for all Matrix class
*************** *************** *******/
template<typena me DataType>
class MatrixInterface
{
public:
template<typena me T>
friend ostream & operator<<(ostr eam &os, MatrixInterface <Tconst &
matrix);
protected:
virtual void PrintDebugMessa ge(ostream &os) const= 0;
};
template<typena me DataType>
ostream & operator<<(ostr eam &os, MatrixInterface <DataTypecons t
&matrix)
{
matrix.PrintDeb ugMessage(os);
return os;
}
/*************** *************** ******
** Simple Matrix
*************** *************** *******/
template<typena me DataType>
class SimpleMatrix:pu blic MatrixInterface <DataType>
{
public:
SimpleMatrix();
SimpleMatrix(si ze_t nrow, size_t ncol);
virtual ~SimpleMatrix() ;
protected:
virtual void PrintDebugMessa ge(ostream &os) const;
size_t m_rowSize, m_colSize;
DataType *m_data;
};
//
//
template<typena me DataType>
SimpleMatrix<Da taType>::Simple Matrix()
:m_rowSize(0), m_colSize(0),
m_data(0)
{
}
//
//
template<typena me DataType>
SimpleMatrix<Da taType>::Simple Matrix(size_t nrow, size_t ncol)
:m_rowSize(nrow ), m_colSize(ncol) ,
m_data(0)
{
if(m_rowSize*m_ colSize>0) m_data =new
DataType[m_rowSize*m_col Size];
}
//
//
template<typena me DataType>
SimpleMatrix<Da taType>::~Simpl eMatrix()
{
if(m_data != 0) delete [] m_data;
}
//
// Default PrintDebugMessa ge
//
template<typena me DataType>
void SimpleMatrix<Da taType>::PrintD ebugMessage(ost ream &os) const
{
size_t index;
cout << typeid(DataType ).name()<<"[ ";
for(index = 0; index < m_rowSize*m_col Size; ++index)
cout<<m_data[index]<<" ";
cout << "]";
}
/*!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!
* I want PrintDebugMessa ge to behave differently
* when I when the m_data submatrice of type = SimpleMatrix<Tw here T
* can be anything.
*
* Doesn't work :(
*
!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!*/
/*
template<typena me T>
void SimpleMatrix< SimpleMatrix<T: :PrintDebugMess age(ostream &os)
const
{
size_t index;
cout << typeid(SimpleMa trix<int>).name ()<<"[[[[ ";
for(index = 0; index < m_rowSize*m_col Size; ++index)
cout<<m_data[index]<<" ";
cout << "]]]]";
}
*/
/*!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!!
* This works. Don't really get it.
!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!*/
template<>
void SimpleMatrix< SimpleMatrix<in t::PrintDebugMe ssage(ostream &os)
const
{
size_t index;
cout << "< "<<typeid(Simpl eMatrix<int>).n ame()<< " "<<"//** ";
for(index = 0; index < m_rowSize*m_col Size; ++index)
cout<<m_data[index]<<" ";
cout << "**//";
}
/*************** ***
*************** ***/
int main(int argc, char **argv)
{
SimpleMatrix<in tIntMatrix(3,3) ;
SimpleMatrix<do ubleDoubleMatri x(2,2);
SimpleMatrix< SimpleMatrix<in t IntMatrixMatrix (2,2);
SimpleMatrix< SimpleMatrix<do uble DoubleMatrixMat rix(2,2);
cout<<IntMatrix <<endl;
cout<<DoubleMat rix<<endl;
cout<<IntMatrix Matrix<<endl;
cout<<DoubleMat rixMatrix<<endl ; //use wrong Print function
return 0;
}