-
August 24th, 2009, 07:47 AM
#1
Design problem with a virtual template class
Hi to all,
I'm new to this forum, nice to meet you.
Let me explain my problem: I am from Java. Yes, I know there is no cure for this but please be patient with me, I'm a Java programmer not due to my will.
Ok, I stop kidding, here it is my real problem:
I want to make a class which act as a Matrix for avoiding the direct use of multidimensional arraies. I've taken the example of the C++ Faq Lite but I wanted to make it more Object Oriented (at least this was mine intention).
So I made a template class Matrix and made it as pure virtual, then two implementation: ArrayMatrix and VectorMatrix (actually unimplemented), the first one use an array hidden in it and the second use a std::vector.
I stumbled in a great pain of compile errors and also many mistakes, after a week of yelling to my computer I found that templates and virtual functions cannot be combined.
Now my question is: can I make in some way a interface (which is pure virtual but not a template) and some implementations (which are templates) somehow?
More in depth:
As taken from the C++ Faq Lite I liked the idea to overload the operator() and at the beginning my code was something like this one (is incomplete an obliviously not functional):
[i]Matrix.h[7i]:
Code:
template<typename T> class Matrix {
public:
Matrix(unsigned int rows, unsigned int cols) throw (Eccezione)
: _righe(rows), _colonne(cols) {}
virtual ~Matrix();
virtual T& operator()(unsigned int row, unsigned int col) throw (Eccezione) = 0;
private:
const T& operator[](int);
protected:
unsigned int _righe;
unsigned int _colonne;
};
class BadSize : public Eccezione
{
public:
BadSize(const std::string& messaggio) : Eccezione(messaggio) {}
};
class IndexOutOfBound : public Eccezione
{
public:
IndexOutOfBound(const std::string& messaggio) : Eccezione(messaggio) {}
};
[i]Eccezione.h[7i]:
Code:
#include <exception>
#include <string>
using std::string;
class Eccezione : public std::exception {
public:
Eccezione();
Eccezione(const string& messaggio) : errore(messaggio) {}
~Eccezione() throw() {}
const char* what() const throw() { return errore.c_str(); }
protected:
string errore;
};
and one of my implementation, ArrayMatrix.h:
Code:
#include "Matrix.h"
template<typename T> class ArrayMatrix : public Matrix<T>
{
public:
ArrayMatrix(unsigned int rows, unsigned int cols) throw (Eccezione) : Matrix<T>(rows, cols)
{
if (rows == 0 || cols == 0)
throw BadSize("Impossile istanziare una Matrice di 0 righe o colonne.\n");
_data = new T[Matrix<T>::_righe * Matrix<T>::_colonne];
}
~ArrayMatrix()
{
delete[] _data;
}
T& operator()(unsigned int row, unsigned int col) throw (Eccezione)
{
if (row > Matrix<T>::_righe || col > Matrix<T>::_colonne)
throw IndexOutOfBound("Indice di riga o colonna non valido");
return _data[row * Matrix<T>::_colonne + col];
}
private:
T* _data;
};
I can't find a way to make an interface and then the implementation as a Matrix due to the redefinition of the operator().
If I remove the template from the class Matrix then the operator() will become:
Code:
virtual Matrix& operator()(unsigned int row, unsigned int col) throw (Eccezione) = 0;
isn't it?
But this will make the interface useless (because the implementations must return a Matrix class).
Am I wrong?
Is there an solutions or do I have to drop the interface and make every implementations as a solo class with no inheritance?
I hope I've been clear, forgive me for my English.
Thank you in advance for your replies.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|