Hello Experts!!
I have two small classes called Intvektor and Matris shown at the bottom and
a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.
Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem
Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically works fine.
The destructor where I delete all the allocated memory also works fine.
Now to my problem I want to be able to use an overloaded indexoperator on my
matris object.
In the main program I want to be able to write a[0][0] = 1; meaning putting
the number 1
in row number index 0 and column number index 0. The object a is an instance
of class Matris.
Both definitions of the Intvektor class and the definition of the Matris
class can be seen at the bottom.
I wrote an overloaded indexoperator in the Intvektor class but that one was
very easy that you can see in the class definition of Intvektor.
So have you any idea how to write an overloaded indexoperator for the Matris
class?
I have no idea so I try to ask you out there.
Many thanks
//Tony
//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
a[0][0] = 1; //I want this to work
return 0;
}
//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk) //default Constructor and user
defined constructor
{ array = new int[size]; }
~Intvektor() //Destruktor
{ delete array; }
Intvektor(const Intvektor& v) //Copy konstruktor
{ copy(v); }
int operator[](int i) const //oveloaded indexoperator
{ return array[i]; }
int& operator[](int i) //oveloaded indexoperator
{ return array[i]; }
const Intvektor& operator=(const Intvektor& v) //assignment operator
{
if (this != &v)
{
delete []array;
copy(v);
}
return *this;
}
private:
void Intvektor::copy(const Intvektor& v) //is called from both
copy constructor and assignment operator
{
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array[i] = v.array[i];
}
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{ matris = new Intvektor*[0]; }//default constructor
Matris(int rows, int cols) : c_rows(rows), c_columns(cols) //user
defined Constructor
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris[i] = new Intvektor[c_columns];
}
Matris(const Matris& v) //Copy constructor
{ copy(v) //is located in the private section }
~Matris() //Destruktor
{ remove(); //Is located in the private section }
const Matris& operator=(const Matris& v) // assignment operator
{
if (this != &v)
{
remove();
copy(v);
}
return *this;
}
private:
void copy(const Matris& v) // is called from both copy constructor
and assignemt operator
{
c_rader = v.c_rader;
c_kolumner = v.c_kolumner;
matris = new Intvektor*[c_rader];
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
void remove() // is called from both destructor and assignment
operator
{
for (int i=0; i< c_rader; i++)
delete[] matris[i];
delete[] matris;
}
int c_rows, c_columns;
Intvektor **matris;
};