template specialization question

E

erez0001

hello,

i am trying to write a matrix class for linear algebra.

template <class T,int ROWS,int COLS> class matrix
{
private:
T m_data[COLS*ROWS];
public:
T &operator at(int x,int y) { ...}
....
};


I want to add a special function only to matrix<T,3,3>.
i still want it to inherent everything from the initial "matrix"
class.

if i just specialize it, i.e.
template <class T> matrix<T,3,3> {
public:
matrix<T,3,3> inv(void) {.....};
};
then i will only have inv(), but not everything else ( e.g. at()
function)

if on the other hand i do:
template <class T> matrix<T,3,3> : public matrix<T,3,3>
then i get a recursion ...


how do i solve that ?
 
A

Alan Woodland

hello,

i am trying to write a matrix class for linear algebra.

template <class T,int ROWS,int COLS> class matrix
{
private:
T m_data[COLS*ROWS];
public:
T &operator at(int x,int y) { ...}
....
};


I want to add a special function only to matrix<T,3,3>.
i still want it to inherent everything from the initial "matrix"
class.

if i just specialize it, i.e.
template <class T> matrix<T,3,3> {
public:
matrix<T,3,3> inv(void) {.....};
};
then i will only have inv(), but not everything else ( e.g. at()
function)

if on the other hand i do:
template <class T> matrix<T,3,3> : public matrix<T,3,3>
then i get a recursion ...


how do i solve that ?
There are two ways to achieve this really:

The usual solution is inheritance when inv() must be a member function,
so you could put your at() function in another class called MatrixBase
or something like that and then have your Matrix inherit that.

Your default Matrix might add nothing over MatrixBase, but
specialisations of it could add other things such as inv().

Alternatively if inv() can be implemented as a free function using the
public interface of Matrix then this might be a good approach also.

Alan
 
N

Neelesh

hello,

i am trying to write a matrix class for linear algebra.

template <class T,int ROWS,int COLS> class matrix
{
private:
    T m_data[COLS*ROWS];
public:
    T &operator at(int x,int y) { ...}

What is "operator at"?
 
E

erez0001

i am trying to write a matrix class for linear algebra.
template <class T,int ROWS,int COLS> class matrix
{
private:
    T m_data[COLS*ROWS];
public:
    T &operator at(int x,int y) { ...}
....
};
I want to add a special function only to matrix<T,3,3>.
i still want it to inherent everything from the initial "matrix"
class.
if i just specialize it, i.e.
template <class T> matrix<T,3,3> {
public:
     matrix<T,3,3> inv(void) {.....};
};
then i will only have inv(), but not everything else ( e.g. at()
function)
if on the other hand i do:
template <class T> matrix<T,3,3> : public matrix<T,3,3>
then i get a recursion ...
how do i solve that ?

There are two ways to achieve this really:

The usual solution is inheritance when inv() must be a member function,
so you could put your at() function in another class called MatrixBase
or something like that and then have your Matrix inherit that.

Your default Matrix might add nothing over MatrixBase, but
specialisations of it could add other things such as inv().

Alternatively if inv() can be implemented as a free function using the
public interface of Matrix then this might be a good approach also.

Alan

alan:
thanks, i'll try that.

neelesh:
sorry, there is no operator at (AFAIK).
that's what happens when i think faster than i write,
i made a mix of function at() and operator[]

thanks,
erez.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top