Hello qt & c++ fans,
I have some probs overloading the [] operator of my templated grid class.
The template class looks basically like this:
template<class T> class Grid
{
public:
Grid(int width, int height) : _pMat(NULL), _iWidth(0), _iHeight(0){
_iWidth = width;
_iHeight = height;
_pMat = createGrid(width, height);
}
~Grid(void){
destroyGrid();
}
Matrix2Dim(const Matrix2Dim<T> &mat){//cpy...}
Matrix2Dim<T>& operator=(const Matrix2Dim<T> &mat){//cpy...}
private:
T** createGrid(int width, int height){
T **pMat = NULL;
pMat = new T *[height];
pMat[0] = new T [height * width];
for (int i = 0; i < height; i++)
pMat[i] = &pMat[0][(i*width)];
return pMat;
}
void destroyGrid(void){
if(_pMat != NULL){
delete _pMat[0];
delete[] _pMat;
_pMat = NULL;
_iWidth = _iHeight = 0;
}
}
int _iWidth;
int _iHeight;
T** _pMat;
};
template<class T> class Grid
{
public:
Grid(int width, int height) : _pMat(NULL), _iWidth(0), _iHeight(0){
_iWidth = width;
_iHeight = height;
_pMat = createGrid(width, height);
}
~Grid(void){
destroyGrid();
}
Matrix2Dim(const Matrix2Dim<T> &mat){//cpy...}
Matrix2Dim<T>& operator=(const Matrix2Dim<T> &mat){//cpy...}
private:
T** createGrid(int width, int height){
T **pMat = NULL;
pMat = new T *[height];
pMat[0] = new T [height * width];
for (int i = 0; i < height; i++)
pMat[i] = &pMat[0][(i*width)];
return pMat;
}
void destroyGrid(void){
if(_pMat != NULL){
delete _pMat[0];
delete[] _pMat;
_pMat = NULL;
_iWidth = _iHeight = 0;
}
}
int _iWidth;
int _iHeight;
T** _pMat;
};
To copy to clipboard, switch view to plain text mode
now I'd like to access data of the class with the [] operator:
Grid<int> *test = new Grid<int>(2, 2);
test[1][1] = 1;
std::cout << test[1][1] << std::endl;
delete test;
Grid<int> *test = new Grid<int>(2, 2);
test[1][1] = 1;
std::cout << test[1][1] << std::endl;
delete test;
To copy to clipboard, switch view to plain text mode
everything I tried to overload the [] operator failed :/, so maybe somebody here knows how to do it???
T *operator[](int value) {
return ???;
}
const T *operator[](int value) const{
return ???;
}
T *operator[](int value) {
return ???;
}
const T *operator[](int value) const{
return ???;
}
To copy to clipboard, switch view to plain text mode
thanks and regards
darksaga
Bookmarks