J
Jason
/*
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 PrintDebugMessage(ostream &os); then I can implement
the function on individual Matrix classes later.
I create a Matrix class called SimpleMatrix, and implemented
PrintDebugMessage(). Everthing works great until I want to implement
different behavior depending whether it's
template<typename DataType> void
SimpleMatrix<DataType>:rintDebugMessage(...); //this is fine
or
//error
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:
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<typename DataType>
class MatrixInterface
{
public:
template<typename T>
friend ostream & operator<<(ostream &os, MatrixInterface<T> const &
matrix);
protected:
virtual void PrintDebugMessage(ostream &os) const= 0;
};
template<typename DataType>
ostream & operator<<(ostream &os, MatrixInterface<DataType> const
&matrix)
{
matrix.PrintDebugMessage(os);
return os;
}
/************************************
** Simple Matrix
*************************************/
template<typename DataType>
class SimpleMatrixublic MatrixInterface<DataType>
{
public:
SimpleMatrix();
SimpleMatrix(size_t nrow, size_t ncol);
virtual ~SimpleMatrix();
protected:
virtual void PrintDebugMessage(ostream &os) const;
size_t m_rowSize, m_colSize;
DataType *m_data;
};
//
//
template<typename DataType>
SimpleMatrix<DataType>::SimpleMatrix()
:m_rowSize(0), m_colSize(0),
m_data(0)
{
}
//
//
template<typename DataType>
SimpleMatrix<DataType>::SimpleMatrix(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_colSize];
}
//
//
template<typename DataType>
SimpleMatrix<DataType>::~SimpleMatrix()
{
if(m_data != 0) delete [] m_data;
}
//
// Default PrintDebugMessage
//
template<typename DataType>
void SimpleMatrix<DataType>:rintDebugMessage(ostream &os) const
{
size_t index;
cout << typeid(DataType).name()<<"[ ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "]";
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* I want PrintDebugMessage to behave differently
* when I when the m_data submatrice of type = SimpleMatrix<T> where T
* can be anything.
*
* Doesn't work
*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*
template<typename T>
void SimpleMatrix< SimpleMatrix<T> >:rintDebugMessage(ostream &os)
const
{
size_t index;
cout << typeid(SimpleMatrix<int>).name()<<"[[[[ ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "]]]]";
}
*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* This works. Don't really get it.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
template<>
void SimpleMatrix< SimpleMatrix<int> >:rintDebugMessage(ostream &os)
const
{
size_t index;
cout << "< "<<typeid(SimpleMatrix<int>).name()<< " > "<<"//** ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "**//";
}
/******************
******************/
int main(int argc, char **argv)
{
SimpleMatrix<int> IntMatrix(3,3);
SimpleMatrix<double> DoubleMatrix(2,2);
SimpleMatrix< SimpleMatrix<int> > IntMatrixMatrix(2,2);
SimpleMatrix< SimpleMatrix<double> > DoubleMatrixMatrix(2,2);
cout<<IntMatrix<<endl;
cout<<DoubleMatrix<<endl;
cout<<IntMatrixMatrix<<endl;
cout<<DoubleMatrixMatrix<<endl; //use wrong Print function
return 0;
}
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 PrintDebugMessage(ostream &os); then I can implement
the function on individual Matrix classes later.
I create a Matrix class called SimpleMatrix, and implemented
PrintDebugMessage(). Everthing works great until I want to implement
different behavior depending whether it's
template<typename DataType> void
SimpleMatrix<DataType>:rintDebugMessage(...); //this is fine
or
//error
template said::rintDebugMessage(...);
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 said::rintDebugMessage(...);
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<typename DataType>
class MatrixInterface
{
public:
template<typename T>
friend ostream & operator<<(ostream &os, MatrixInterface<T> const &
matrix);
protected:
virtual void PrintDebugMessage(ostream &os) const= 0;
};
template<typename DataType>
ostream & operator<<(ostream &os, MatrixInterface<DataType> const
&matrix)
{
matrix.PrintDebugMessage(os);
return os;
}
/************************************
** Simple Matrix
*************************************/
template<typename DataType>
class SimpleMatrixublic MatrixInterface<DataType>
{
public:
SimpleMatrix();
SimpleMatrix(size_t nrow, size_t ncol);
virtual ~SimpleMatrix();
protected:
virtual void PrintDebugMessage(ostream &os) const;
size_t m_rowSize, m_colSize;
DataType *m_data;
};
//
//
template<typename DataType>
SimpleMatrix<DataType>::SimpleMatrix()
:m_rowSize(0), m_colSize(0),
m_data(0)
{
}
//
//
template<typename DataType>
SimpleMatrix<DataType>::SimpleMatrix(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_colSize];
}
//
//
template<typename DataType>
SimpleMatrix<DataType>::~SimpleMatrix()
{
if(m_data != 0) delete [] m_data;
}
//
// Default PrintDebugMessage
//
template<typename DataType>
void SimpleMatrix<DataType>:rintDebugMessage(ostream &os) const
{
size_t index;
cout << typeid(DataType).name()<<"[ ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "]";
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* I want PrintDebugMessage to behave differently
* when I when the m_data submatrice of type = SimpleMatrix<T> where T
* can be anything.
*
* Doesn't work
*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*
template<typename T>
void SimpleMatrix< SimpleMatrix<T> >:rintDebugMessage(ostream &os)
const
{
size_t index;
cout << typeid(SimpleMatrix<int>).name()<<"[[[[ ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "]]]]";
}
*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* This works. Don't really get it.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
template<>
void SimpleMatrix< SimpleMatrix<int> >:rintDebugMessage(ostream &os)
const
{
size_t index;
cout << "< "<<typeid(SimpleMatrix<int>).name()<< " > "<<"//** ";
for(index = 0; index < m_rowSize*m_colSize; ++index)
cout<<m_data[index]<<" ";
cout << "**//";
}
/******************
******************/
int main(int argc, char **argv)
{
SimpleMatrix<int> IntMatrix(3,3);
SimpleMatrix<double> DoubleMatrix(2,2);
SimpleMatrix< SimpleMatrix<int> > IntMatrixMatrix(2,2);
SimpleMatrix< SimpleMatrix<double> > DoubleMatrixMatrix(2,2);
cout<<IntMatrix<<endl;
cout<<DoubleMatrix<<endl;
cout<<IntMatrixMatrix<<endl;
cout<<DoubleMatrixMatrix<<endl; //use wrong Print function
return 0;
}