tags:

views:

46

answers:

1

I have a matrix class that takes the row and column reference type that can be used to access it as template parameters. Meanwhile, the row and column types are passed the matrix type that they are representing.

Is there any way to break this circularity? Here are some demonstrative code snippets:

#include "MatrixConcreteType.h"
template <class MatrixType>
class rowType <MatrixType>
{...}

And in the matrix file:

#include "VectorTypes.h"
template <class row_t, class col_t>
class Matrix
{...}

I suppose I could try a well placed extern?

+1  A: 

If I am understanding your question correctly, you are trying to define Matrix as a template taking a row type and a column type, and then the rowType (and i assume also columnType) as templates of a matrix type. My big question is: where is the actual data? At some point, I presume this Matrix should actually break down to a structured group of ints, or doubles, or chars, or reverse iterators of pointers to bools, or something. Where is that?

your circularity seems to be a symptom of trying to make too much architecture. Figure out which class you want to actually store the data in, make the template parameter of that class the type of that data, then make the template parameter of all other related classes either the main class, or the data type as well. for example:

template <class DataType>
class Matrix{
    //store the data in here: a DataType [], or vector<DataType>, or something
}

and:

template <class MatrixType>
class Row{
    //has something which refers back to the original matrix of type MatrixType
}

or

template <class DataType>
class Row{
    //has something which refers back to the original matrix of type Matrix<DataType>
}

I would suggest using the second of the above alternatives, as it makes is easier to refer to the Row class in the Matrix class, such as:

template <class DataType>
Row<DataType> Matrix::getRow(int index){
    //return an instance of Row<DataType> containing the appropriate row
}
tlayton